Servlet笔记6--Servlet程序改进

第一步改进,GenericServlet:

  我们目前所有放入Servlet类直接实现了javax.servlet.Servlet接口,但是这个接口中有很多方法是目前不需要的,我们可能只需要编写service。直接实现Servlet接口代码丑陋,有必要在中间添加一个适配器,以后所有的Servlet类不再直接实现Servlet接口,应该去继承适配器类。

  适配器除了可以让代码优雅之外,我们还可以在适配器中提供大量的方法,子类继承之后,可以在子类中直接使用,方便编程。

  自己使用缺省适配器设计模式实现的GenericServlet类:

 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;

 import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; /*
* GenericSerrvlet是一个适配器,这个适配器是一个Servlet
* 以后javaweb程序员无需直接实现Servlet接口,去继承这个适配器即可,重写service方法。
*
* @author Administrator
*/ public abstract class GenericServlet implements Servlet { ServletConfig config; //该方法不能被覆盖,因为被覆盖之后,config就获取不到了,所以方法前面加了final
public final void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
} /*
* 若在初始化时刻需要执行一段特殊的代码,建议重写无参数的init方法,注意动态绑定,会让上面的init方法调用子类重写的init方法
*/
public void init(){ } public abstract void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException; public void destroy() { } public ServletConfig getServletConfig() {
return config;
} public String getServletInfo() {
return "qjj";
} //---------------------以下所有的方法都是拓展方法,方便子类的使用------------------------------ public ServletContext getServletContext(){
return getServletConfig().getServletContext();
}
}

  其实sun公司已经为我们实现了这个适配器类,可以直接调用:

 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; //public class HelloServlet extends GenericServlet {
public class HelloServlet extends javax.servlet.GenericServlet {
//sun公司已经为我们写了适配器,并且里面有很多其他的方法 private static final long serialVersionUID = 1L; @Override
public void init() {
System.out.println("HelloServlet's init......");
} @Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("hello world!");
out.print("<br>");
out.print(getServletContext());
} }

HTTP协议相关:

Servlet笔记6--Servlet程序改进

Servlet笔记6--Servlet程序改进

Servlet笔记6--Servlet程序改进

Servlet笔记6--Servlet程序改进

Servlet笔记6--Servlet程序改进

  前端页面中的GET请求和POST请求代码:

  <html>
<head>
<title>login page</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body> GET方式
<form action="/prj-servlet-09/system/login">
username<input type="text" name="username"><br>
password<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
<hr>
GET方式
<form action="/prj-servlet-09/system/login" method="get">
username<input type="text" name="username"><br>
password<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
<hr>
POST方式
<form action="/prj-servlet-09/system/login" method="post">
username<input type="text" name="username"><br>
password<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body> </html>

第二步改进,HTTPServlet:

Servlet笔记6--Servlet程序改进

  具体代码实现:

 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginServlet extends GenericServlet { private static final long serialVersionUID = 1L; @Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
//将ServletRequest,ServletResponse强制类型转换成带有Http的接口类型
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res; response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); //获取浏览器发送的请求方式
String method = request.getMethod();
//LoginServlet是处理登录的,要求前端必须发送POST请求
if("GET".equals(method)){
//前端报错
out.print("405-您应当发送POST请求");
//后端报错
throw new RuntimeException("405-您应当发送POST请求");
} //若程序执行到这里证明用户发送到的请求是POST请求,程序应当正常执行
out.print("正在登录...");
} }

Servlet笔记6--Servlet程序改进

  解决方法:自己用模板方法设计模式设计HTTPServlet:

 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class HttpServlet extends GenericServlet { private static final long serialVersionUID = 1L; @Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
service(request,response);
} public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getMethod();
if("GET".equals(method)){
doGet(request,response);
}else if("POST".equals(method)){
doPost(request,response);
}
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("405-您应当发送POST请求");
throw new RuntimeException("405-您应当发送POST请求");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("405-您应当发送GET请求");
throw new RuntimeException("405-您应当发送GET请求");
} }

  接下写自己的Servlet时,要继承自HttpServlet,为get请求则重写doGet方法,为post请求则重写doPost方法。

  其实sun公司已经为我们实现了这个HttpServlet类,可以直接调用:

 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;

 import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //public class LoginServlet extends HttpServlet {
public class LoginServlet extends javax.servlet.http.HttpServlet { private static final long serialVersionUID = 1L; @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().print("login......");
}
}

  至此,以后写自己的Servlet时,都要继承自HttpServlet,并且使用doPost或者doGet方法。

上一篇:修改Ubuntu默认运行级别,启动字符界面


下一篇:在 Ubuntu 14.04 中配置 PXE 服务器