Servlet(9)—HttpServlet和改进Servlet实例

时间:2023-03-09 19:08:16
Servlet(9)—HttpServlet和改进Servlet实例

  1. HttpServlet:针对Http协议定义的一个Servlet基类,唯一的功能就是强制类型转换ServletRequest转换成HttpServletRequest,ServletResponse转换成HttpServletResponse,HttpServlet继承自GenericServlet,而GenericServlet实现了Servlet接口和ServletConfig接口。也就是说编写Servlet类不需要直接实现Servlet类,去继承HttpServlet就行。
  2. 好处:
    • 不会产生多余无用的代码
    • 不用在强制类型转换了(为了获取请求的方式是get还是post需要用到需要用request.getMethod(),其中request必须是HttpServletRequest类型的,HttpServlet在service(ServletRequest req, ServletResponse res)已经强制类型转换好了)
    • 在service(HttpServletRequest req, HttpServletResponse resp)重载方法中获取请求方式,get请求会调用doget方法,post请求会执行dopost方法。所以我们可以再这2个方法中定义我们自己的业务逻辑

3 . Http中的方法:

HttpServlet()

doGet(HttpServletRequest, HttpServletResponse)

getLastModified(HttpServletRequest)

doHead(HttpServletRequest, HttpServletResponse)

doPost(HttpServletRequest, HttpServletResponse)

doPut(HttpServletRequest, HttpServletResponse)

doDelete(HttpServletRequest, HttpServletResponse)

getAllDeclaredMethods(Class< ? >)

doOptions(HttpServletRequest, HttpServletResponse)

doTrace(HttpServletRequest, HttpServletResponse)

service(HttpServletRequest, HttpServletResponse)

maybeSetLastModified(HttpServletResponse, long)

service(ServletRequest, ServletResponse)

改进后的实例:

HttpServlet

/**
* 针对Http协议定义的一个Servlet基类
*唯一的功能就是强制类型转换ServletRequest转换成HttpServletRequest,ServletResponse转换成
*HttpServletResponse
*/
public class MyHttpServlet extends MyGenericServlet { @Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
if(arg0 instanceof HttpServletRequest){
HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;
if(arg1 instanceof HttpServletResponse){
HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;
service(httpServletRequest, httpServletResponse);
}
}
} public void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
//1.获取请求方式
String method = arg0.getMethod(); //2.根据请求的方式再调用对应的处理方法
if("GET".equalsIgnoreCase(method)){
doGet(arg0, arg1);
}else if("POST".equalsIgnoreCase(method)){
doPost(arg0, arg1);
}
} public void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{ } public void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException{ }
}

Servlet:

public class LoginServlet3 extends MyHttpServlet {

    @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求的方式
String method = request.getMethod();
System.out.println("请求方式:" + method); //1.获取请求参数username,password(获取的是表单信息)
String username = request.getParameter("username");
String password = request.getParameter("password"); //2.获取当前web应用的初始化参数user,password。
String initUser = getServletContext().getInitParameter("user");
String initPassword = getServletContext().getInitParameter("password"); PrintWriter out = response.getWriter();
//3.比对
if(initUser.equals(username) && initPassword.equals(password)){
//4.打印响应字符串
out.println("Hello: " + username);
}else{
out.println("Sorry: " + username);
}
} @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求的方式
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String method = httpServletRequest.getMethod();
System.out.println("请求方式:" + method); //1.获取请求参数username,password(获取的是表单信息)
String username = request.getParameter("username");
String password = request.getParameter("password"); //2.获取当前web应用的初始化参数user,password。
String initUser = getServletContext().getInitParameter("user");
String initPassword = getServletContext().getInitParameter("password"); PrintWriter out = response.getWriter();
//3.比对
if(initUser.equals(username) && initPassword.equals(password)){
//4.打印响应字符串
out.println("Hello: " + username);
}else{
out.println("Sorry: " + username);
}
}
}

页面:

<body>

    <form action="loginServlet" method="get">
user:<input type="text" name="user"/>
<br>
password:<input type="password" name="password"/>
<br>
interesting:
<input type="checkbox" name="interesting" value="read"/>read
<input type="checkbox" name="interesting" value="walk"/>walk
<input type="checkbox" name="interesting" value="swing"/>swing
<input type="checkbox" name="interesting" value="shopping"/>shopping
<input type="checkbox" name="interesting" value="tv"/>TV
<br>
<input type="submit" value="Submit"/>
</form>
</body>