码迷,mamicode.com
首页 > 编程语言 > 详细

转发.隐藏JSP.URL地址.md

时间:2017-07-07 20:07:02      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

一、转发参数:

1.将jsp里面的参数通过LoginServlet转到PageSelvert:

@WebServlet(“/login”) public class LoginServlet extends HttpServlet{ protected void dopost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ //转发到PageServlet去 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对象里,设置属性值,只要是同一个request对象才能获得此数据(所以转发可以,重定向不行,重定向是两个) //将email带过去(只能在前面,如是在传的后(getRequestDisPatcher后面),则得不到) 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){ //得到123456@163.com String email = (String)request.getAttribute(“email”); //删除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>

20170707001

目的:隐藏jsp 将访问页面改成如下:

<welcome-file-list> <welcome-file>/WEB-INF/pages/index.jsp</welcome-file> </welcome-file-list>

20170707001

20170707001

前面加“/”直接到以其为根目录的地方

//admin为一个虚拟夹子 @WebServlet("/admin/test") public class TestServlet entends HttpServlet{ protected void deGet(HttpServletQuest req,HttpServletResponse resp)throws ServletException,IOEception{
resp.sendRedirect("index.jsp");
}
}

20170707001

想访问到的3种方法:

//”..”代表在index.jsp目录下向上跳一个目录 resp.sendRedirect("../index.jsp");
System.out.println(req.getContextPath());//servlet7_url 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);

如果不转码,直接打印,就会出现乱码,如下图:

20170707001

如果想将一个Servlet里面的字符传到另一个Servlet中去,需要进行转码,如:

Request.sendRedirect(text1?name=”狗子”);

此时应写成:

Text0Servlet:
Request.sendRedirect(“text1?name=”+URLEncode.encode(“狗子”));
Text1Servlet: //tomcat8 Request.setCharacterEncoding(“UTF-8”); //tomcat 7 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"); //String s=new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8") System.out.println(req.getParameter);

转发.隐藏JSP.URL地址.md

标签:

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
凯哥学堂
加入时间:2016-10-07
  关注此人  发短消息
java学习视频下载:www.kaige123.com
凯哥学堂”关注的人------(0
凯哥学堂”的粉丝们------(1
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!