[原创]java WEB学习笔记10:GenericServlet

时间:2024-08-22 08:06:01

本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.实现页面的跳转:对于LoginServlet.java 采用直接实现的Servlet接口的方式

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 配置web应用的初始化参数 -->
<context-param>
<param-name>user</param-name>
<param-value>jason</param-value>
</context-param> <context-param>
<param-name>password</param-name>
<param-value>zhangzhen</param-value>
</context-param> <!-- 配置和映射servlet -->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.jason.testweb.LoginServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern> <!-- 和提表单提交的 action 一致 -->
</servlet-mapping> </web-app>

login.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>this is a test </title>
</head>
<body>
<form action="login" method="post"> user:<input type="text" name="username"/>
password:<input type="text" name="password"/> <input type="submit" value="submit"/> </form>
</body>
</html>

LoginServlet.java

 package com.jason.testweb;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class LoginServlet implements Servlet { @Override
public void destroy() {
// TODO Auto-generated method stub
} @Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
} @Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
} private ServletConfig servletConfig; //自定义一个成员变量 @Override
public void init(ServletConfig servletConfig) throws ServletException { this.servletConfig = servletConfig; } @Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// 1.获取请求参数
String userName = request.getParameter("username");
String passWord = request.getParameter("password"); // 2.获取当前web 应用的初始化参数,user,password
ServletContext servletContext = servletConfig.getServletContext();
String initUser = servletContext.getInitParameter("user");
String initPassword = servletContext.getInitParameter("password"); PrintWriter out = response.getWriter(); // 3.比对
// 4.打印响应字符串
if (initUser.equals(userName) && initPassword.equals(passWord)) {
out.print("hello :" + userName);
} else {
out.print("sorry :" + userName);
} } }

2.实现页面的跳转:创建一个MyGenericServlet的类,实现Servlet接口,对于 LoginServlet.java则继承MyGenericServlet类

MyGenericServlet.java

 package com.jason.testweb;

 import java.io.IOException;
import java.util.Enumeration; import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; /**
*
* @author:jason
* @time:2016年5月18日
* @description:自定义的Servlet 接口的实现类,,简化开发
*
*/ public abstract class MyGenericServlet implements Servlet { @Override
public void destroy() {
} @Override
public ServletConfig getServletConfig() {
return servletConfig;
} private ServletConfig servletConfig; // 声明一个成员变量 @Override
public String getServletInfo() {
return null;
} @Override
public void init(ServletConfig arg0) throws ServletException {
this.servletConfig = arg0;
} @Override
// 声明为抽象方法,由每个继承的类的实现业务
public abstract void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException; }

LoginServlet.java

 package com.jason.testweb;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class LoginServlet extends MyGenericServlet { @Override
      //重写抽象方法
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// 1.获取请求参数
String userName = request.getParameter("username");
String passWord = request.getParameter("password"); // 2.获取当前web 应用的初始化参数,user,password
ServletContext servletContext = getServletConfig().getServletContext();
String initUser = servletContext.getInitParameter("user");
String initPassword = servletContext.getInitParameter("password"); PrintWriter out = response.getWriter(); // 3.比对
// 4.打印响应字符串
if (initUser.equals(userName) && initPassword.equals(passWord)) {
out.print("hello :" + userName);
} else {
out.print("sorry :" + userName);
} } }

2.关于GenericServlet

  public abstract class GenericServlet  extends java.lang.Object   implements    Servlet, ServletConfig, java.io.Serializable

[原创]java WEB学习笔记10:GenericServlet

1)是一个Servlet。 是Servlet 接口和ServletConfig 接口的实现类。但是是一个抽象类。其中service 方法为抽象方法

2)如果新建的Servlet 程序直接继承GenericServlet 更加简洁

3)具体实现

①在GenericServlet 中声明了一个ServletConfig 类型的成员变量,在init(ServletConfig) 方法中对其初始化。

 ②不建议直接覆盖init(ServletConfig) ,因为如果调用super(ServletConfig),而还有ServletConfig接口的方法,则抛出空指针异常。int(){}方法不是sevlet的声明周期方法。而init(ServletConfig)是servlet的周期方法

   @Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
} /**
* A convenience method which can be overridden so that there's no need to
* call <code>super.init(config)</code>.
* <p>
* Instead of overriding {@link #init(ServletConfig)}, simply override this
* method and it will be called by
* <code>GenericServlet.init(ServletConfig config)</code>. The
* <code>ServletConfig</code> object can still be retrieved via
* {@link #getServletConfig}.
*
* @exception ServletException
* if an exception occurs that interrupts the servlet's
* normal operation
*/
public void init() throws ServletException {
// NOOP by default
}

②利用了servletConfig 成员变量的方法实现了ServletConfig 接口的方法(以下是GenericServlet 的方法源码)

  @Override
public String getInitParameter(String name) {
return getServletConfig().getInitParameter(name);
} @Override
public Enumeration<String> getInitParameterNames() {
return getServletConfig().getInitParameterNames();
} @Override
public ServletConfig getServletConfig() {
return config;
} @Override
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
} @Override
public String getServletName() {
return config.getServletName();
}

3.总结

 1)深刻理解servletConfig对象

 2)java的封装,继承特性