1-3servlet源码

JavaWeb项目部署
2017年11月18日
spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException
2017年11月28日

index.jsp

  1. <%@ page language=“java”
    import=“java.util.*” pageEncoding=“ISO-8859-1”%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
  8. <html>
  9. <head>
  10. <base href=”<%=basePath%>”>
  11.  
  12. <title>My JSP ‘index.jsp’ starting page</title>
  13.     <meta http-equiv=”pragma” content=”no-cache”>
  14.     <meta http-equiv=”cache-control” content=”no-cache”>
  15.     <meta http-equiv=”expires” content=”0″>
  16.     <meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
  17.     <meta http-equiv=”description” content=”This is my page”>
  18.     <!–
  19.     <link rel=”stylesheet” type=”text/css” href=”styles.css”>
  20.     –>
  21. </head>
  22.  
  23. <body>
  24. This is my JSP page. <br>
  25. </body>
  26. </html>

Login.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Login.html</title>
  5.     
     
  6. <meta
    http-equiv=“keywords”
    content=“keyword1,keyword2,keyword3”>
  7. <meta
    http-equiv=“description”
    content=“this is my page”>
  8. <meta
    http-equiv=“content-type”
    content=“text/html; charset=UTF-8”>
  9.  

  10. <!–<link rel=”stylesheet” type=”text/css” href=”./styles.css”>–>
  11.  
  12. </head>
  13.  
  14. <body>
  15. This is my Login page. <br>
  16. <form
    name = “LoginForm”
    method =“post”
    action=“Login”>
  17. 用户名:<input
    type =“text”
    name =“userName”><br><br>
  18. 密码:<input
    type =“password”
    name=“password”><br><br>
  19. <input
    type=“submit”
    value=“登录”>
  20.  
  21. <br><br><br><br><br>
  22. <a
    href = “login?userName=abcdef”>测试get链接</a>
  23. </form>
  24. </body>
  25. </html>

web.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app
    version=“3.0”
  3.     xmlns=“http://java.sun.com/xml/ns/javaee”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”>
  7. <display-name>JSPforstudent</display-name>    
  8. <welcome-file-list>
  9. <welcome-file>Login.html</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <context-param>
  14.     <param-name>testContextParam1</param-name>
  15.     <param-value>testContextValue1</param-value>
  16. </context-param>
  17.  
  18. <context-param>
  19.     <param-name>testContextParam2</param-name>
  20.     <param-value>testContextValue2</param-value>
  21. </context-param>
  22.  

  23. <!–REBUILDSERVLET servlet 配置 –>
  24. <servlet>
  25.     <servlet-name>rebuildServlet</servlet-name>
  26.     <servlet-class>sch.cauc.study.sysmanage.RebuildServlet</servlet-class>     
  27. </servlet>
  28.  
  29. <servlet-mapping>
  30.     <!– 引用哪个servlet –>
  31. <servlet-name>rebuildServlet</servlet-name>

  32. <!– 客户端访问servlet的名称 –>
  33. <url-pattern>/rebuild</url-pattern>
  34. </servlet-mapping>
  35.  

  36. <!– servlet 配置格式 –>
  37. <servlet>
  38. <servlet-name>testServlet</servlet-name>
  39. <servlet-class>sch.cauc.study.sysmanage.TestServlet</servlet-class>
  40. </servlet>
  41.  
  42. <servlet-mapping>

  43. <!– 引用那个servl –>
  44. <servlet-name>testServlet</servlet-name>

  45. <!– 客户端访问servlet的名称 –>
  46. <url-pattern>/test</url-pattern>
  47.  
  48. </servlet-mapping>
  49.  

  50. <!– servlet 配置格式 –>
  51. <servlet>
  52. <servlet-name>LoginServlet</servlet-name>
  53. <servlet-class>sch.cauc.study.sysmanage.LoginServlet</servlet-class>
  54. </servlet>
  55.  
  56. <servlet-mapping>

  57. <!– 引用那个servl /映射–>
  58. <servlet-name>LoginServlet</servlet-name>

  59. <!– 客户端访问servlet的名称/匹配 –>
  60. <url-pattern>/Login</url-pattern>
  61. </servlet-mapping>
  62.  

  63. <!– servlet 配置格式 –>
  64. <servlet>
  65. <servlet-name>TestServletOther</servlet-name>
  66. <servlet-class>sch.cauc.study.sysmanage.TestServletOther</servlet-class>
  67. </servlet>
  68. <servlet-mapping>

  69. <!– 引用那个servl /映射–>
  70. <servlet-name>TestServletOther</servlet-name>

  71. <!– 客户端访问servlet的名称/匹配 –>
  72. <url-pattern>/Other</url-pattern>
  73. </servlet-mapping>
  74.  

  75. <!– filter 配置格式 –>
  76. <filter>
  77. <filter-name>characterEncodingFilter</filter-name>
  78. <filter-class>sch.cauc.study.sysmanage.Filter.CharacterEncodingFilter</filter-class>

  79. <!– 配置初始化参数 –>
  80.     <init-param>
  81.         <param-name>encoding</param-name>
  82.         <param-value>UTF-8</param-value>
  83.     </init-param>
  84. </filter>
  85. <filter-mapping>

  86. <!– –>
  87. <filter-name>characterEncodingFilter</filter-name>

  88. <!–如果是/*,则对此路径下所有servlet起作用,如果/servlet,则只对此servlet起作用 –>
  89. <url-pattern>/*</url-pattern>
  90. </filter-mapping>
  91.  

  92. <!– filter 配置格式 –>
  93. <filter>
  94. <filter-name>TestFilter1</filter-name>
  95. <filter-class>sch.cauc.study.sysmanage.Filter.TestFilter1</filter-class>

  96. <!– 配置初始化参数 –>
  97.     <init-param>
  98.         <param-name>encoding</param-name>
  99.         <param-value>UTF-8</param-value>
  100.     </init-param>
  101. </filter>
  102. <filter-mapping>

  103. <!– –>
  104. <filter-name>TestFilter1</filter-name>

  105. <!–如果是/*,则对此路径下所有servlet起作用,如果/servlet,则只对此servlet起作用 –>
  106. <url-pattern>/*</url-pattern>
  107. </filter-mapping>
  108. </web-app>

TestServlet.java

  1. package sch.cauc.study.sysmanage;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4.  
  5. import javax.security.auth.message.callback.PrivateKeyCallback.Request;
  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. import com.sun.mail.iap.Response;
  12. public
    class
    TestServlet
    extends
    HttpServlet{
  13.  
  14.     private
    static
    final
    long serialVersionUID=1L;
  15.     /**
  16.      *
  17.      * 相应get请求方式
  18.      * 方法名:doGet
  19.      * 1.直接访问servlet 2.直接在url上面写参数
  20.      * java里面初始化就是new
  21.      */
  22.     
  23.     protected
    void
    doGet(HttpServletRequest req, HttpServletResponse resp)
  24.             throws ServletException, IOException {
  25.         System.out.println();
  26.     System.out.println(“I AM DOGET”);
  27.     System.out.println(“这是相对路径: “+req.getRequestURI());
  28.     System.out.println(“工程名: “+req.getContextPath());
  29.     System.out.println(“这是绝对路径: “+req.getRequestURL());
  30.     System.out.println(“客户端IP地址: “+req.getRemoteAddr());
  31.     System.out.println(“服务器IP地址: “+req.getRemoteHost());
  32.     
  33.     resp.setContentType(“text/html;charset=utf-8”);
  34.     //获取浏览器输出流(最原始的方法)
  35.     PrintWriter out =resp.getWriter();
  36.     //数据库查询
  37.     out.println(“<h1><div>我的第一个servlet</dic></h1>”);
  38.     out.println(“<span>xxxxxxxxx</span>”);
  39.     out.flush();
  40.     out.close();
  41.         
  42.     }
  43.     /**
  44.      *
  45.      * 响应post请求方式
  46.      * 方法名:doPost
  47.      * 创建人:xrzhang
  48.      * @param req
  49.      * @param resp
  50.      * @throws ServletException
  51.      * @throws IOException void
  52.      * @exception

  53.      * @since 1.0.0
  54.      */
  55.  
  56.     
  57.     protected
    void
    doPost(HttpServletRequest req, HttpServletResponse resp)
  58.             throws ServletException, IOException {
  59.         System.out.println(“I AM DOPOST”);
  60.     
  61.     }
  62.  
  63. /**
  64. *
  65. * 注销servlet实例
  66. * 方法名:destroy
  67. * 创建人:xrzhang
  68.  
  69. * @exception

  70. * @since 1.0.0
  71. */
  72.     public
    void
    destroy() {
  73.     
     
  74.         System.out.println(“I AM destory”);
  75.     }
  76.  
  77.     /**
  78.      *
  79.      * servlet初始化方法
  80.      * 方法名:init
  81.      */
  82.     public
    void
    init() throws ServletException {
  83.         
  84.         System.out.println(“I AM init”);
  85.     }
  86.  
  87.     /**
  88.      * 第一个测试的servlet类
  89.      * 方法名:main
  90.      */
  91.     
  92.     public
    static
    void
    main(String[] args) {
  93.         
  94.  
  95.     }
  96.  
  97. }

 

RebuildServlet.java

  1. package sch.cauc.study.sysmanage;
  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. *
  12. *
  13. * RebuildServlet
  14. * 创建人:xrzhang
  15. * @version 1.0.0
  16. *
  17. */
  18. public
    class
    RebuildServlet
    extends
    HttpServlet{
  19.  
  20.     private
    static
    final
    long serialVersionUID = 1L;
  21.  
  22.     
  23.     protected
    void
    doGet(HttpServletRequest request, HttpServletResponse response)
  24.             throws ServletException, IOException {
  25.      System.out.println(” I AM DOGET METHOD “);
  26.     System.out.println(” I AM DOGET METHOD “);
  27.     System.out.println(“servlet相对路径:”+request.getRequestURI());
  28.     System.out.println(“servlet绝对路径:”+request.getRequestURL());
  29.     System.out.println(“工程名”+request.getContextPath());
  30.     System.out.println(“客户端IP地址:”+request.getRemoteAddr());
  31.     System.out.println(“服务器IP地址”+request.getRemoteHost());
  32.     
  33.     //获取浏览器输出流
  34.     response.setContentType(“text/html;charset=utf-8”);
  35.     PrintWriter out = response.getWriter();
  36.     out.println(“<h1><div>我的第一个Servlet应用程序!!!</div></h1>”);
  37.     out.println(“<span>xxxxxxxxxxxxx</span>”);
  38.     out.flush();
  39.     out.close();
  40.     }
  41.  
  42.     
  43.     protected
    void
    doPost(HttpServletRequest req, HttpServletResponse resp)
  44.             throws ServletException, IOException {
  45.          System.out.println(” I AM DOGET METHOD “);
  46.     }
  47.  
  48.  
  49.     public
    void
    destroy() {
  50.          System.out.println(” I AM DESTORY METHOD “);
  51.     }
  52.  
  53.     
  54.     public
    void
    init() throws ServletException {
  55.          System.out.println(” I AM init METHOD “);
  56.  
  57.     }
  58.     
     
  59.     
     
  60.     
  61. }

 TestServletOther.java

  1. package sch.cauc.study.sysmanage;
  2. import java.io.IOException;
  3.  
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. /**
  9. *
  10. *
  11. * TestServletOther
  12. * 创建人:xrzhang
  13. * @version 1.0.0
  14. *
  15. */
  16. public
    class
    TestServletOther
    extends
    HttpServlet {
  17.     private
    static
    final
    long serialVersionUID=1L;
  18.  
  19.  
  20.     protected
    void
    doGet(HttpServletRequest req, HttpServletResponse resp)
  21.             throws ServletException, IOException {
  22.     
  23.     }
  24.  
  25.     
  26.     protected
    void
    doPost(HttpServletRequest req, HttpServletResponse resp)
  27.             throws ServletException, IOException {
  28.     
  29.     }
  30.  
  31.     
  32.     public
    void
    destroy() {
  33.         
  34.     }
  35.  
  36.     public
    void
    init() throws ServletException {
  37.         
  38.     }
  39.     
     
  40.     
  41. }

LoginServlet.java

  1. package sch.cauc.study.sysmanage;
  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. *
  12. *
  13. * LoginServlet
  14. * 创建人:xrzhang
  15. * @version 1.0.0
  16. *
  17. */
  18. public
    class
    LoginServlet
    extends
    HttpServlet{
  19.  
  20.     private
    static
    final
    long serialVersionUID = 1L;
  21.     
  22.     protected
    void
    doGet(HttpServletRequest req, HttpServletResponse resp)
  23.             throws ServletException, IOException {
  24.         System.out.println(“I AM DOGET METHOD!”);
  25.         //将所有动作引入doPost里面
  26.         doPost(req, resp);
  27.     }
  28.  
  29.     
  30.     protected
    void
    doPost(HttpServletRequest req, HttpServletResponse resp)
  31.             throws ServletException, IOException {
  32.         System.out.println(“I AM DOPOST METHOD!”);
  33.          /*
  34.          request.setCharacterEncoding(“UTF-8”);
  35.          response.setCharacterEncoding(“UTF-8”);
  36.          response.setContentType(“text/html;charset=UTF-8”);
  37.          先把中文字符串转化为字节,再把字节以指定的编码组合
  38.          String userNameChange = new String(userName.getBytes(“iso-8859-1″),”UTF-8”);
  39.          */
  40.         req.setCharacterEncoding(“UTF-8”);
  41.         String userName=req.getParameter(“userName”);
  42.         String password=req.getParameter(“password”);
  43.         
  44.          resp.setCharacterEncoding(“UTF-8”);
  45.          resp.setContentType(“text/html;charset=UTF-8”);
  46.         PrintWriter out =resp.getWriter();
  47.         out.print(“<h1>用户名为:”+userName+“</h1>”);
  48.         out.print(“<h1>密码为:”+password+“</h1>”);
  49.         out.flush();
  50.         out.close();
  51.     }
  52.  
  53.     
  54.     public
    void
    destroy() {
  55.         System.out.println(“I AM DESTORY METHOD!”);
  56.     }
  57.  
  58.     
  59.     public
    void
    init() throws ServletException {
  60.         System.out.println(“I AM INIT METHOD!”);
  61.     }
  62.     
     
  63.  
  64. }

CharacterEncodingFilter.java

  1. package sch.cauc.study.sysmanage.Filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11.  
  12.  
  13.  
  14. public
    class
    CharacterEncodingFilter
    implements
    Filter{
  15.  
  16.     private FilterConfig filterConfig;
  17.  
  18.  
  19.     
  20.     public
    void
    destroy() {
  21.         
  22.         
  23.     }
  24.  
  25.     
  26.     public
    void
    doFilter(ServletRequest request, ServletResponse response,
  27.             FilterChain chain) throws IOException, ServletException {
  28.         //获取filter初始化的参数值
  29.         String encoding =filterConfig.getInitParameter(“encoding”);
  30.         if(null!=encoding&&!encoding.equals(“”)){
  31.             request.setCharacterEncoding(encoding);
  32.             response.setCharacterEncoding(encoding);
  33.             response.setContentType(“text/html;charset=”+encoding);
  34.         }
  35.         System.out.println(“我们进入了字符编码转化的过滤器”);
  36.         //把请求和响应继续带给下一个filter,如果没有下一个filter就进入请求的servlet(下一步)
  37.         chain.doFilter(request, response);
  38.     }
  39.  
  40.     
  41.     public
    void
    init(FilterConfig config) throws ServletException {
  42.         this.filterConfig=config;
  43.         
  44.     }
  45.  
  46. }

TestFilter1.java

  1. package sch.cauc.study.sysmanage.Filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11.  
  12. public
    class
    TestFilter1
    implements
    Filter{
  13. private FilterConfig filterConfig ;
  14.     
  15.     public
    void
    destroy() {
  16.         
  17.     }
  18.  
  19.     public
    void
    doFilter(ServletRequest request, ServletResponse response,
  20.             FilterChain chain) throws IOException, ServletException {
  21.     
     
  22.         System.out.println(“我们进入了TestFilter1的过滤器”);
  23.         //把请求和响应继续带个下一个filter,如果没有下一个filter就进入请求的servlet(下一步)
  24.         chain.doFilter(request, response);
  25.     }
  26.     
     
  27.     
  28.     public
    void
    init(FilterConfig config) throws ServletException {
  29.         this.filterConfig = config;
  30.     }
  31.     
     
  32.  
  33. }

发表评论

电子邮件地址不会被公开。 必填项已用*标注