文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习三十【原创】  


Java Servlet

手写Servlet项目,发布的目录结构


http://127.0.0.1:8090/anan/Hello

.

├── WEB-INF

   ├── classes

      ├── HelloServlet.class

      └── HelloServlet.java

   └── web.xml

└── index.html


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class HelloServlet extends HttpServlet {


@Override

public void doGet(HttpServletRequest request, HttpServletResponse response)

        throws IOException, ServletException {

        PrintWriter out = response.getWriter();

        out.println("<h1>HelloServlet</h1>");

}

}


web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

  version="3.1"

  metadata-complete="true">


  <display-name>Welcome to Tomcat</display-name>

  <description>

     Welcome to Tomcat

  </description>


  <servlet>

        <servlet-name>HelloServlet</servlet-name>

        <servlet-class>HelloServlet</servlet-class>

  </servlet>


  <servlet-mapping>

        <servlet-name>HelloServlet</servlet-name>

        <url-pattern>/Hello</url-pattern>

  </servlet-mapping>


</web-app>


javaEE只提供了标准,没有提供实现;实现是容器自己实现的;tomcat.jar都放在了tomcat lib的下面; 目录:/usr/local/Cellar/tomcat/8.5.15/libexec/lib

编译:javac -classpath /usr/local/Cellar/tomcat/8.5.15/libexec/lib/servlet-api.jar HelloServlet.java

重启tomcat就可以看到项目了


Eclipse

创建动态Web项目,需要添加tomcat配置(new runtime


Servlet 声明周期:

init 方法 【初始化Servlet

service 方法 【处理请求的】一般不需要重写 重写doGET doPOST的方法即可

destory方法【监听的时候去用】

我们也可以直接继承 GenericService不继承HttpService,方法比较少。


index.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<form action="RegisterServlet" method="post">

姓名:<input type="text" name="username"><br />

密码:<input type="password" name="password"><br />

爱好:<input type="checkbox" name="hobby" value="tv">Tv

<input type="checkbox" name="hobby" value="sport">Sport

<input type="checkbox" name="hobby" value="read">Read

<input type="submit" value="注册">

</form>

<br />

<a href="RegisterServlet?username=testGet&password=123456&hobby=sport&hobby=read">GET提交</a>

</body>

</html>


RegisterServlet

package Test;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class RegisterServlet

 */

public class RegisterServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public RegisterServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

request.setCharacterEncoding("utf-8"); //post

response.setContentType("text/html;charset=utf-8");

String user_name = request.getParameter("username");

String user_pass = request.getParameter("password");

String[] hobby = request.getParameterValues("hobby");

PrintWriter data = response.getWriter();

data.println("<h1>Hello</h1><br />");

data.println("name:" + user_name + "---pass:" + user_pass + "<br />");

for(String h: hobby) {

data.println(h + ",");

}

data.println("<br />");

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}


}


response.sendReadirect(“”); //跳转