Servlet学习--练习示例总结

时间:2022-10-16 23:55:53

醉了醉了。。本来想测试下Servlet生命周期的,然后调了好久的错误,还是没成功,不知道为什么不能这样做

贴上代码:

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LifeServlet extends HttpServlet{ private static final long serialVersionUID = 1L;
StringBuffer str;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
str.append("处理Servlet请求-1");
doPost(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { str.append("处理Servlet请求-2"); response.setCharacterEncoding("utf-8");
PrintWriter out;
out = response.getWriter();
out.println("<html>");
out.println("<head><title>fuck you</title></head>");
out.println(str);
out.println("</html>");
out.close();
} @Override
public void destroy() {
str.append("销毁Servlet");
} @Override
public void init() {
str.append("初始化Servlet");
} }

我只是想不在控制台打印出来而已,要不要这样啊

另外测试了下客户端跳转request请求是不行的,服务端跳转都可以

贴上客户端跳转 request,session,application三种请求代码示例

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class RedirectServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("requestKey", "requestֵ");
HttpSession session=request.getSession();
session.setAttribute("sessionKey", "sessionֵ");
ServletContext application=this.getServletContext();
application.setAttribute("applicationKey", "applicationֵ");
response.sendRedirect("target.jsp");
}
}

另外目标地址(target.jsp)的文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<h1>目标地址</h1>
request值:<%=request.getAttribute("requestKey") %><br/>
session值:<%=session.getAttribute("sessionKey") %><br/>
application值:<%=application.getAttribute("applicationKey") %><br/>
</body>
</html>

OK,下面贴上服务器跳转对应三种请求的代码示例:

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class ForwardServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("requestKey", "requestֵ");
HttpSession session=request.getSession();
session.setAttribute("sessionKey", "sessionֵ");
ServletContext application=this.getServletContext();
application.setAttribute("applicationKey", "applicationֵ");
RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
rd.forward(request, response);
}
}

可以看出服务器端跳转只是

	RequestDispatcher rd=request.getRequestDispatcher("target.jsp");
rd.forward(request, response);

和客户端的不同,所以个人理解也就是客户端跳转应该是服务器通过response返回给客户端,然后客户端对应跳转url

另外:

贴个比较好的理解

1.forward跳转:

a.服务器端跳转,地址栏不改变;

b.执行到跳转语句后马上无条件跳转,之后的代码不再执行(跳转之前一定要释放全部资源);

c.request设置的属性在跳转后的页面仍可以使用;

d.使用<jsp:param name="参数名" value="参数值" />传递参数。



2.response跳转:

a.客户端跳转,地址栏改变;

b.所有代码执行完毕后跳转;

c.跳转后的页面不能使用上一个页面的request属性;

d.使用地址重写传递参数(response.sendRedirect("URL?参数名=参数值"))。

说实话里面的细节我也不懂,知道有这么回事就行了