开发学院WEB开发Jsp JSP利用URL重写传递参数示例 阅读

JSP利用URL重写传递参数示例

 2009-10-25 16:35:23 来源:WEB开发网 闂傚倸鍊风欢姘缚瑜嶈灋闁圭虎鍠栫粻顖炴煥閻曞倹瀚�闂傚倸鍊风粈渚€骞夐敓鐘插瀭闁汇垹鐏氬畷鏌ユ煙閹殿喖顣奸柛搴$У閵囧嫰骞掗幋婵冨亾閻㈢ǹ纾婚柟鐐灱濡插牊绻涢崱妤冃℃繛宀婁簽缁辨捇宕掑鎵佹瀸闂佺懓鍤栭幏锟�濠电姷鏁告慨顓㈠箯閸愵喖宸濇い鎾寸箘閹规洟姊绘笟鈧ḿ褍煤閵堝悿娲Ω閳轰胶鍔﹀銈嗗笂閼冲爼鍩婇弴銏$厪闁搞儮鏅涙禒褏绱掓潏鈺佷槐闁轰焦鎹囬弫鎾绘晸閿燂拷闂傚倸鍊风欢姘缚瑜嶈灋闁圭虎鍠栫粻顖炴煥閻曞倹瀚�  闂傚倸鍊烽懗鑸电仚缂備胶绮〃鍛村煝瀹ュ鍗抽柕蹇曞У閻庮剟姊虹紒妯哄闁诲繑姘ㄩ埀顒佸嚬閸撶喎顫忓ú顏勫瀭妞ゆ洖鎳庨崜浼存⒑闁偛鑻晶顔剧磼婢跺﹦绉虹€殿喖顭锋俊姝岊槷闁稿鎹囧Λ鍐ㄢ槈濞嗗繑娈橀梻浣风串缂嶁偓濞存粠鍓熼崺鈧い鎺戝€归弳顒勬煕鐎n亷韬€规洑鍗冲鍊燁槾闁哄棴绠撻弻銊╂偆閸屾稑顏�
核心提示:在servlet页中重定向到index.jsp页,但重写index.jsp页的URL传递两个参数给index页 1.Servlet页: /** * * @author lucifer */ package MyServlet;import java.io.IOException;import java.io.PRint

在servlet页中重定向到index.jsp页,但重写index.jsp页的URL传递两个参数给index页

1.Servlet页:


/**
 *
 * @author lucifer
 */


package MyServlet;

import java.io.IOException;
import java.io.PRintWriter;
import javax.servlet.*;
import javax.servlet.http.*;


public class URLRewritingServlet extends HttpServlet {
 
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
     response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
     String urI = response.encodeRedirectURL("http://localhost:8084/LearnServlet/index.jsp?sid=5748&name=hell");
     response.sendRedirect( urI );

   try {
       out.println("<html>");
       out.println("<head>");
       out.println("<title>Servlet URLRewritingServlet</title>"); 
       out.println("</head>");
       out.println("<body>");
       out.println("<h1>Servlet URLRewritingServlet at " + request.getContextPath () + "</h1>");
       out.println("</body>");
       out.println("</html>");
     } finally {
       out.close();
     }
   }

   @Override
   public void init(ServletConfig config)throws ServletException{
     super.init(config);
   }

   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
     processRequest(request, response);
   }

   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
     processRequest(request, response);
   }

   @Override
   public String getServletInfo() {
     return "Short description";
   }

}

2.index.jsp:

<%--
   Document  : index
   Created on : 2009-10-13, 12:59:28
   Author   : lucifer
--%>

<%@page contentType="text/html" 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>JSP Page</title>
   </head>
   <body>
     <%
        String name = request.getParameter("name");
        String a = request.getParameter("sid");
        out.print(a + "<br>" + name);
     %>
   </body>
</html>


3.输出:

5748

hell

Tags:JSP 利用 URL

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接