Servlet开发

Posted zyxsblogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet开发相关的知识,希望对你有一定的参考价值。

Servlet开发

Servlet引入:

百度百科详细servlet介绍;

重点概括:servlet运行在服务器上,处理用户请求。

我们使用response来获取一个写方法 PrintWriter out=response.getWriter(); out.print("要输出的信息") 输出值到页面中。

 在HelloWorldServlet类中我们输出信息。

技术分享图片
 1 package com.java1234.web;
 2 import java.io.IOException;
 3 import java.io.PrintWriter;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 public class HelloWorldServlet extends HttpServlet {
 9 
10     /**
11      * 
12      */
13     private static final long serialVersionUID = 1L;
14 
15     @Override
16     protected void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         // TODO Auto-generated method stub
19         this.doPost(request, response);
20     }
21 
22     @Override
23     protected void doPost(HttpServletRequest request, HttpServletResponse response)
24             throws ServletException, IOException {
25         // TODO Auto-generated method stub
26         response.setCharacterEncoding("gbk");
27         PrintWriter out = response.getWriter();
28         out.print("<html>");
29         out.print("<head><title>Servlet开发</title></head>");
30         out.print("问候servlet tmd");
31         out.print("</html>");
32         out.close();
33     }
34 
35 }
HelloWorldServlet.java

web.xml文件配置如下:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>HeadFristJspServlet05</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13     <servlet-name>HelloWorldServlet</servlet-name>
14     <servlet-class>com.java1234.web.HelloWorldServlet</servlet-class>
15   </servlet>
16   <servlet-mapping>
17     <servlet-name>HelloWorldServlet</servlet-name>
18     <url-pattern>/helloWorld</url-pattern>
19   </servlet-mapping>
20 </web-app>
web.xml

这个配置知识我们在学习JSP九大内置对象----config的时候学习过配置的基本过程和访问的时候跳转次序。

Servlet的生命周期

简单的概括分为四步:servlet类加载--->实例化---->服务----->销毁

我们使用一个例子看一下完整的过程

首先建一个类LifeServlet.java

技术分享图片
 1 package com.java1234.web;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class LifeServlet extends HttpServlet{
12 
13     /**
14      * 
15      */
16     private static final long serialVersionUID = 1L;
17 
18     @Override
19     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
20             throws ServletException, IOException {
21         // TODO Auto-generated method stub
22         System.out.println("service服务开始啦");
23         this.doPost(request,response);
24     }
25 
26     @Override
27     protected void doPost(HttpServletRequest request, HttpServletResponse response) 
28             throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         PrintWriter out=response.getWriter();
31         out.print("servcice提供服务");
32         System.out.println("servcice提供服务!");
33     }
34 
35     @Override
36     public void destroy() {
37         // TODO Auto-generated method stub
38         System.out.println("销毁service");
39     }
40 
41     @Override
42     public void init() throws ServletException {
43         // TODO Auto-generated method stub
44         System.out.println("实例化service");
45     }
46     
47 }
LifeServlet.java

web.xml配置如下:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>HeadFristJspServlet05</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13       <servlet-name>LifeServlet</servlet-name>
14       <servlet-class>com.java1234.web.LifeServlet</servlet-class>
15   </servlet>
16   <servlet-mapping>
17       <servlet-name>LifeServlet</servlet-name>
18       <url-pattern>/life</url-pattern>
19   </servlet-mapping>
20 </web-app>
web.xml

当我们客户端访问life时

会出现:

技术分享图片

当我们在一段时间内不使用或者访问的时候。容器认为servlet没啥鸟用了 所以就销毁。。。。

技术分享图片

当然啦!init(初始化只会有一次)

 这个我们可以从这里看到(我们在客户端请求了两次):

技术分享图片

 

客户端跳转VS服务器端跳转

1、在Servlet中获取session,application

2、客户端跳转 response.sendRedirect(“目标地址”);

我们无法获取request的信息。

使用的类是

RedirectServlet.java 代码如下

技术分享图片
 1 package com.java1234.web;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11 public class RedirectServlet extends HttpServlet{
12 
13     /**
14      * 
15      */
16     private static final long serialVersionUID = 1L;
17 
18     @Override
19     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
20             throws ServletException, IOException {
21         // TODO Auto-generated method stub
22         this.doPost(request, response);
23     }
24 
25     @Override
26     protected void doPost(HttpServletRequest request, HttpServletResponse response)
27             throws ServletException, IOException {
28         // TODO Auto-generated method stub
29         request.setAttribute("requestKey", "request取值");//获取request
30         HttpSession session=request.getSession();
31         session.setAttribute("sessionKey", "session取值");//获取session
32         ServletContext application=this.getServletContext();
33         application.setAttribute("applicationKey", "applicaiton取值");//获取applicaiton
34         response.sendRedirect("target-redirect.jsp");//客户端跳转/重定向
35     }
36 }
RedirectServlet.java

跳转的目标文件:

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>target-redirect</title>
 8 </head>
 9 <body>
10     <h1>Servlet取值</h1>
11     <h6>request值:<%=request.getAttribute("requestKey") %></h6>
12     <h6>session值:<%=session.getAttribute("sessionKey") %></h6>
13     <h6>application值:<%=application.getAttribute("applicationKey") %></h6>
14 </body>
15 </html>
target-redirect.jsp

web.xml配置信息:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>HeadFristJspServlet05</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13     <servlet-name>HelloWorldServlet</servlet-name>
14     <servlet-class>com.java1234.web.HelloWorldServlet</servlet-class>
15   </servlet>
16   <servlet-mapping>
17     <servlet-name>HelloWorldServlet</servlet-name>
18     <url-pattern>/helloWorld</url-pattern>
19   </servlet-mapping>
20   <servlet>
21       <servlet-name>LifeServlet</servlet-name>
22       <servlet-class>com.java1234.web.LifeServlet</servlet-class>
23   </servlet>
24   <servlet-mapping>
25       <servlet-name>LifeServlet</servlet-name>
26       <url-pattern>/life</url-pattern>
27   </servlet-mapping>
28   <servlet>
29       <servlet-name>RedirectServlet</servlet-name>
30       <servlet-class>com.java1234.web.RedirectServlet</servlet-class>
31   </servlet>
32   <servlet-mapping>
33       <servlet-name>RedirectServlet</servlet-name>
34       <url-pattern>/redirect</url-pattern>
35   </servlet-mapping>
36 </web-app>
web.xml

技术分享图片

 

3、服务器跳转:使用的关键代码 RequestDispatcher rd=request.getRequestdispatcher(“目标地址”);rd.forward(request,response);

 文件为 ForwardServlet.java

技术分享图片
 1 package com.java1234.web;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.RequestDispatcher;
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 public class ForwardServlet extends HttpServlet{
13 
14     /**
15      * 
16      */
17     private static final long serialVersionUID = 1L;
18 
19     @Override
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
21             throws ServletException, IOException {
22         // TODO Auto-generated method stub
23         this.doPost(request, response);
24     }
25 
26     @Override
27     protected void doPost(HttpServletRequest request, HttpServletResponse response)
28             throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         request.setAttribute("requestKey", "request取值");//获取request
31         HttpSession session=request.getSession();
32         session.setAttribute("sessionKey", "session取值");//获取session
33         ServletContext application=this.getServletContext();
34         application.setAttribute("applicationKey", "applicaiton取值");//获取applicaiton
35         RequestDispatcher rd=request.getRequestDispatcher("target-redirect.jsp");
36         rd.forward(request, response);
37     }
38 }
ForwardServlet.java

目标文件:

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>target-redirect</title>
 8 </head>
 9 <body>
10     <h1>Servlet取值服务器</h1>
11     <h6>request值:<%=request.getAttribute("requestKey") %></h6>
12     <h6>session值:<%=session.getAttribute("sessionKey") %></h6>
13     <h6>application值:<%=application.getAttribute("applicationKey") %></h6>
14 </body>
15 </html>
target-redirect.jsp

配置如下:

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>HeadFristJspServlet05</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   <servlet>
13     <servlet-name>HelloWorldServlet</servlet-name>
14     <servlet-class>com.java1234.web.HelloWorldServlet</servlet-class>
15   </servlet>
16   <servlet-mapping>
17     <servlet-name>HelloWorldServlet</servlet-name>
18     <url-pattern>/helloWorld</url-pattern>
19   </servlet-mapping>
20   <servlet>
21       <servlet-name>LifeServlet</servlet-name>
22       <servlet-class>com.java1234.web.LifeServlet</servlet-class>
23   </servlet>
24   <servlet-mapping>
25       <servlet-name>LifeServlet</servlet-name>
26       <url-pattern>/life</url-pattern>
27   </servlet-mapping>
28   <servlet>
29       <servlet-name>RedirectServlet</servlet-name>
30       <servlet-class>com.java1234.web.RedirectServlet</servlet-class>
31   </servlet>
32   <servlet-mapping>
33       <servlet-name>RedirectServlet</servlet-name>
34       <url-pattern>/redirect</url-pattern>
35   </servlet-mapping>
36   <servlet>
37   <servlet-name>ForwardServlet</servlet-name>
38       <servlet-class>com.java1234.web.ForwardServlet</servlet-class>
39   </servlet>
40   <servlet-mapping>
41       <servlet-name>ForwardServlet</servlet-name>
42       <url-pattern>/forward</url-pattern>
43   </servlet-mapping>
44 </web-app>
web.xml

结果为

技术分享图片

客户端和服务器对比一目了然!!

以上是关于Servlet开发的主要内容,如果未能解决你的问题,请参考以下文章

java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理(代码片段

filter与servlet

VSCode插件开发全攻略代码片段设置自定义欢迎页

IOS开发-OC学习-常用功能代码片段整理

jsp初识servlet转发转发与重定向的比较

servlet和filter的区别