一、转发参数:
1.将jsp里面的参数通过LoginServlet转到PageSelvert:
@WebServlet(“/login”)
public class LoginServlet extends HttpServlet{
protected void dopost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
Request.getRequestDispatcher(“/page”),forword(request,reapomse);
}}
@WebServlet(“page”)
public class PageServlet extends HttpServlet{
protected void dopost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String usename = request.getParameter(“usename”);
String password = request.getParameter(“password”);
System.out.pritln(usename);
System.out.pritln(passworrd);
}
}
2.将LoginServlet里面的存放的值带给PageServlet中去:
@WebServlet(“/login”)
Public class LoginServlet extends HttpServlet{
Protected void dopost(HttpServletRequest request,HttpServletResponse response){
Request.setAttribute(“email”,”123456@163.com”);
Request.getRequestDisPatcher(“/page”),forword(request,response);
}
}
@WebServlet(“/page”)
Public class PageServlet extends HttpServlet{
Protected void dopost(HttpServletRequest request,HttpServletResponse response){
String email = (String)request.getAttribute(“email”);
Request.removeAttribute(“email”);
Request.getAttributeNames();
String usename = request.getParameter(“usename”);
String password = request.getParameter(“password”);
}
}
转发参数:
removeAttribute 删除
getAttributeNames 拿到所有的名字
setAttribute 设置值
getAttribute 得到值
request
response
他们的生命周期,就在请求和响应之间结束。
二、隐藏JSP:
可以将JSP放入WEB-INF目录,以后只能用转发来访问以下的JSP
<welcome-file-list>
<-- 欢迎页面是转发机制的跳转 -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
目的:隐藏jsp
将访问页面改成如下:
<welcome-file-list>
<welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
</welcome-file-list>
前面加“/”直接到以其为根目录的地方
@WebServlet("/admin/test")
public class TestServlet entends HttpServlet{
protected void deGet(HttpServletQuest req,HttpServletResponse resp)throws ServletException,IOEception{
resp.sendRedirect("index.jsp");
}
}
想访问到的3种方法:
resp.sendRedirect("../index.jsp");
System.out.println(req.getContextPath());
resp.sendRedirect(req.getContextPath()+"/index.jsp");
resp.sendRedirect(req.getContextPath() + "/");
三、乱码问题:
Tomcat7 版本转换乱码需要看方法来转get string类转码 post就直接设置编码就可以了
String s=req.getParament("text");
↓
<from actoin="lm" method="get">
System.out.println(new String(s.getBytes(ISO-8859),"utf-8"));
req,setCharacterEncoding("UTF-8");
String s=req.getParamenter("test");
↓
<from actoin="lm" method="post">
System.out.println(s);
Tomcat8 版本就不需要半段方法,直接设置转码就可
req.setCharacterEncoding("UTF-8");
String s=req.getParameter("test");
System.out.println(s);
如果不转码,直接打印,就会出现乱码,如下图:
如果想将一个Servlet里面的字符传到另一个Servlet中去,需要进行转码,如:
Request.sendRedirect(text1?name=”狗子”);
此时应写成:
Text0Servlet:
Request.sendRedirect(“text1?name=”+URLEncode.encode(“狗子”));
Text1Servlet:
Request.setCharacterEncoding(“UTF-8”);
String s=new String(request.getParameter(“name”).getBytes
(“ISO-8859-1”),”utf-8”);
System.out.println(request.getParameter(“name”));
resp.sendRedirect("test1?name="+URLEncoder.encode("多态","UTF-8"));
↓
req.setCharacterEncoding("UTF-8");
System.out.println(req.getParameter);