由一个例题开始:
web,xml中配置:
<context-param>
<param-name>user</param-name>
<param-value>JXK</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>19980402</param-value>
</context-param>
<servlet>
<servlet-name>SigInServlet</servlet-name>
<servlet-class>day0128.javaweb.SigInServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SigInServlet</servlet-name>
<url-pattern>/SigInServlet</url-pattern>
</servlet-mapping>
html文件中代码:
<body>
<form action="SigInServlet" method="get">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" name="submit">
</form>
</body>
SigInservlet 类中相关代码:
private ServletConfig config = null;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
this.config = servletConfig;
}
@Override
public void service(ServletRequest requst, ServletResponse response) throws ServletException, IOException {
//得到 前端的 username 和 Password
String username = requst.getParameter("username");
String password = requst.getParameter("password");
//得到 初始化的username 和 password
ServletContext servletContext = config.getServletContext();
String initUser = servletContext.getInitParameter("user");
String initPassword = servletContext.getInitParameter("password");
PrintWriter out = response.getWriter();
if (initUser.equals(username) && initPassword.equals(password)) {
out.println("Hello :"+username);
}else {
out.print("Sorry :"+ username);
}
}
过于麻烦!
手动写一个继承了servlet 和 servletConfig 的 myGenericServlet
当实现了servle 接口后
public abstract class MyGenericServlet implements Servlet{
@Override
public void destroy() {}
//Init 方法可以传进 servletConfig 变量,可实现
@Override
public ServletConfig getServletConfig() {
return this.config;
}
@Override
public String getServletInfo() {
return null;
}
//写一个 config
private ServletConfig config = null;
@Override
public void init(ServletConfig config) throws ServletException {
//初始化 config
this.config = config;
}
//是每一个 servlet 必须实现的方法,变成抽象的给子类实现
@Override
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
}
让例题实现这个抽象方法,则在SigInServlet 中仅需要实现 server 即可
public class SigInServlet extends MyGenericServlet{
@Override
public void service(ServletRequest requst, ServletResponse response) throws ServletException, IOException {
//得到 前端的 username 和 Password
String username = requst.getParameter("username");
String password = requst.getParameter("password");
//得到 初始化的username 和 password
ServletContext servletContext = getServletConfig().getServletContext();
String initUser = servletContext.getInitParameter("user");
String initPassword = servletContext.getInitParameter("password");
PrintWriter out = response.getWriter();
if (initUser.equals(username) && initPassword.equals(password)) {
out.println("Hello :"+username);
}else {
out.print("Sorry :"+ username);
}
}
}
注:config 需要变成 getServletConfig() 方法
使 myGenericServlet 继续实现 servlerConfig 接口
public abstract class MyGenericServlet implements Servlet,ServletConfig{
@Override
public void destroy() {}
//Init 方法可以传进 servletConfig 变量,可实现
@Override
public ServletConfig getServletConfig() {
return this.config;
}
@Override
public String getServletInfo() {
return null;
}
//写一个 config
private ServletConfig config = null;
@Override
public void init(ServletConfig config) throws ServletException {
//初始化 config
this.config = config;
}
//是每一个 servlet 必须实现的方法,变成抽象的给子类实现
@Override
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
@Override
public String getInitParameter(String name) {
return config.getInitParameter(name);
}
@Override
public Enumeration<String> getInitParameterNames() {
return config.getInitParameterNames();
}
@Override
public ServletContext getServletContext() {
return config.getServletContext();
}
@Override
public String getServletName() {
return config.getServletName();
}
}
让 SigInServlet 继承 myGenericServlet
public class SigInServlet extends MyGenericServlet{
@Override
public void service(ServletRequest requst, ServletResponse response) throws ServletException, IOException {
//得到 前端的 username 和 Password
String username = requst.getParameter("username");
String password = requst.getParameter("password");
//得到 初始化的username 和 password
String initUser = getServletContext().getInitParameter("user");
String initPassword =getServletContext().getInitParameter("password");
PrintWriter out = response.getWriter();
if (initUser.equals(username) && initPassword.equals(password)) {
out.println("Hello :"+username);
}else {
out.print("Sorry :"+ username);
}
}
}
不需要在获取 servlerContext 对象,直接可以使用 servlerConfig 的 getServletContext() 方法进行java web 应用初始化的调用
查看 GenericServlet的源代码,除了上述包含内容外,源码中还有日志,以及如下init 的初始化
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
public void init() throws ServletException {
// NOOP by default
}
提供了一个有参数的init() 和一个没有参数的init()
子类若直接覆盖了有参数,则会对这个方法进行重写,可能导致空指针异常,不建议/
init() :不是一个生命周期方法
init(ServlerConfig):是一个生命周期方法
HttpServlet:
1).是一个Servlet,继承自GenericServlet.针对于HTTP协议所定制。
2).在service()方法中直接把ServletReuqest和ServletResponse转为HttpServletRequest和HttpServ1etRespons并调用了重载的service(HttpServ1etRequest, HttpServ1etResponse)
在service(HttpServletRequest, HttpServletResponse) 获取了请求方式: request . getMethod().根据请求方式有创建了doXxx()方法(xxx为具体的请求方式,比如doGet, doPost)