本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.实现页面的跳转:对于LoginServlet.java 采用直接实现的Servlet接口的方式
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 7 <!-- 配置web应用的初始化参数 --> 8 <context-param> 9 <param-name>user</param-name> 10 <param-value>jason</param-value> 11 </context-param> 12 13 <context-param> 14 <param-name>password</param-name> 15 <param-value>zhangzhen</param-value> 16 </context-param> 17 18 <!-- 配置和映射servlet --> 19 <servlet> 20 <servlet-name>LoginServlet</servlet-name> 21 <servlet-class>com.jason.testweb.LoginServlet</servlet-class> 22 </servlet> 23 24 <servlet-mapping> 25 <servlet-name>LoginServlet</servlet-name> 26 <url-pattern>/login</url-pattern> <!-- 和提表单提交的 action 一致 --> 27 </servlet-mapping> 28 29 </web-app>
login.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>this is a test </title> 6 </head> 7 <body> 8 <form action="login" method="post"> 9 10 user:<input type="text" name="username"/> 11 password:<input type="text" name="password"/> 12 13 <input type="submit" value="submit"/> 14 15 </form> 16 </body> 17 </html>
LoginServlet.java
1 package com.jason.testweb; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Servlet; 7 import javax.servlet.ServletConfig; 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 13 public class LoginServlet implements Servlet { 14 15 @Override 16 public void destroy() { 17 // TODO Auto-generated method stub 18 } 19 20 @Override 21 public ServletConfig getServletConfig() { 22 // TODO Auto-generated method stub 23 return null; 24 } 25 26 @Override 27 public String getServletInfo() { 28 // TODO Auto-generated method stub 29 return null; 30 } 31 32 private ServletConfig servletConfig; //自定义一个成员变量 33 34 @Override 35 public void init(ServletConfig servletConfig) throws ServletException { 36 37 this.servletConfig = servletConfig; 38 39 } 40 41 @Override 42 public void service(ServletRequest request, ServletResponse response) 43 throws ServletException, IOException { 44 // 1.获取请求参数 45 String userName = request.getParameter("username"); 46 String passWord = request.getParameter("password"); 47 48 // 2.获取当前web 应用的初始化参数,user,password 49 ServletContext servletContext = servletConfig.getServletContext(); 50 String initUser = servletContext.getInitParameter("user"); 51 String initPassword = servletContext.getInitParameter("password"); 52 53 PrintWriter out = response.getWriter(); 54 55 // 3.比对 56 // 4.打印响应字符串 57 if (initUser.equals(userName) && initPassword.equals(passWord)) { 58 out.print("hello :" + userName); 59 } else { 60 out.print("sorry :" + userName); 61 } 62 63 } 64 65 }
2.实现页面的跳转:创建一个MyGenericServlet的类,实现Servlet接口,对于 LoginServlet.java则继承MyGenericServlet类
MyGenericServlet.java
1 package com.jason.testweb; 2 3 import java.io.IOException; 4 import java.util.Enumeration; 5 6 import javax.servlet.GenericServlet; 7 import javax.servlet.Servlet; 8 import javax.servlet.ServletConfig; 9 import javax.servlet.ServletContext; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 14 /** 15 * 16 * @author:jason 17 * @time:2016年5月18日 18 * @description:自定义的Servlet 接口的实现类,,简化开发 19 * 20 */ 21 22 public abstract class MyGenericServlet implements Servlet { 23 24 @Override 25 public void destroy() { 26 } 27 28 @Override 29 public ServletConfig getServletConfig() { 30 return servletConfig; 31 } 32 33 private ServletConfig servletConfig; // 声明一个成员变量 34 35 @Override 36 public String getServletInfo() { 37 return null; 38 } 39 40 @Override 41 public void init(ServletConfig arg0) throws ServletException { 42 this.servletConfig = arg0; 43 } 44 45 @Override 46 // 声明为抽象方法,由每个继承的类的实现业务 47 public abstract void service(ServletRequest arg0, ServletResponse arg1) 48 throws ServletException, IOException; 49 50 }
LoginServlet.java
1 package com.jason.testweb; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.Servlet; 7 import javax.servlet.ServletConfig; 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 13 public class LoginServlet extends MyGenericServlet { 14 15 @Override //重写抽象方法 16 public void service(ServletRequest request, ServletResponse response) 17 throws ServletException, IOException { 18 // 1.获取请求参数 19 String userName = request.getParameter("username"); 20 String passWord = request.getParameter("password"); 21 22 // 2.获取当前web 应用的初始化参数,user,password 23 ServletContext servletContext = getServletConfig().getServletContext(); 24 String initUser = servletContext.getInitParameter("user"); 25 String initPassword = servletContext.getInitParameter("password"); 26 27 PrintWriter out = response.getWriter(); 28 29 // 3.比对 30 // 4.打印响应字符串 31 if (initUser.equals(userName) && initPassword.equals(passWord)) { 32 out.print("hello :" + userName); 33 } else { 34 out.print("sorry :" + userName); 35 } 36 37 } 38 39 }
2.关于GenericServlet
public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
1)是一个Servlet。 是Servlet 接口和ServletConfig 接口的实现类。但是是一个抽象类。其中service 方法为抽象方法
2)如果新建的Servlet 程序直接继承GenericServlet 更加简洁
3)具体实现
①在GenericServlet 中声明了一个ServletConfig 类型的成员变量,在init(ServletConfig) 方法中对其初始化。
②不建议直接覆盖init(ServletConfig) ,因为如果调用super(ServletConfig),而还有ServletConfig接口的方法,则抛出空指针异常。int(){}方法不是sevlet的声明周期方法。而init(ServletConfig)是servlet的周期方法
1 @Override 2 public void init(ServletConfig config) throws ServletException { 3 this.config = config; 4 this.init(); 5 } 6 7 /** 8 * A convenience method which can be overridden so that there‘s no need to 9 * call <code>super.init(config)</code>. 10 * <p> 11 * Instead of overriding {@link #init(ServletConfig)}, simply override this 12 * method and it will be called by 13 * <code>GenericServlet.init(ServletConfig config)</code>. The 14 * <code>ServletConfig</code> object can still be retrieved via 15 * {@link #getServletConfig}. 16 * 17 * @exception ServletException 18 * if an exception occurs that interrupts the servlet‘s 19 * normal operation 20 */ 21 public void init() throws ServletException { 22 // NOOP by default 23 }
②利用了servletConfig 成员变量的方法实现了ServletConfig 接口的方法(以下是GenericServlet 的方法源码)
1 @Override 2 public String getInitParameter(String name) { 3 return getServletConfig().getInitParameter(name); 4 } 5 6 @Override 7 public Enumeration<String> getInitParameterNames() { 8 return getServletConfig().getInitParameterNames(); 9 } 10 11 @Override 12 public ServletConfig getServletConfig() { 13 return config; 14 } 15 16 @Override 17 public ServletContext getServletContext() { 18 return getServletConfig().getServletContext(); 19 } 20 21 @Override 22 public String getServletName() { 23 return config.getServletName(); 24 }
3.总结
1)深刻理解servletConfig对象
2)java的封装,继承特性