轉寄.隱藏JSP.URL地址.md

來源:互聯網
上載者:User

標籤:

一、轉寄參數:

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){ //得到[email protected] 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>

目的:隱藏jsp 將訪問頁面改成如下:

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

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

//admin為一個虛擬夾子 @WebServlet("/admin/test") public class TestServlet entends HttpServlet{ protected void deGet(HttpServletQuest req,HttpServletResponse resp)throws ServletException,IOEception{resp.sendRedirect("index.jsp");}}

想訪問到的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);

如果不轉碼,直接列印,就會出現亂碼,如:

如果想將一個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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.