您的位置:首页 > Web前端 > JavaScript

JSP基础知识(servlet)

2017-09-11 18:39 495 查看
1.servlet

servlet是一个java程序,是在服务器上运行以处理客户端请求并做出响应的程序

servlet生命周期

生命周期各个阶段

实例化--->servlet容器创建servlet的实例

初始化--->该容器调用init()方法

请求处理--->如果请求servlet,则容器调用service()方法

服务终止--->销毁实例之前调用destroy()方法

配置servlet

1.在web.xml中配置servlet

<!-- 配置servlet -->

<servlet>

<!-- 映射名称 -->

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

<!-- 具体的servlet类 -->

<servlet-class>com.jredu.j2ee.ch04.HelloServlet</servlet-class>

</servlet>

<servlet-mapping>

<!-- 映射名称 -->

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

<!-- 映射路径(虚拟路径) -->

<url-pattern>/hello</url-pattern>

</servlet-mapping>

2.使用注解的方式配置servlet

使用注解WebServlet:

Name

urlPatterns、Value:配置servlet

loadOnStartup:添加这个参数后可以设置servlet立即运行

initParams:设置初始化参数

设置输入输出为中文:

//将输入转换为中文

request.setCharacterEncoding("UTF-8");

//设置输出为中文

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

response.setCharacterEncoding("UTF-8");

//name:servlet-name

//value、urlPatterns:url-pattern

@WebServlet(urlPatterns={"/annotation","/a"})

public class AnnotatioonServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public AnnotatioonServlet() {

super();

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");

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

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println(" <BODY>");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the GET method");

out.println(" </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: