您现在的位置: 365建站网 > 365文章 > JSP利用URL重写传递参数示例

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

文章来源:365jz.com     点击数:1006    更新时间:2009-10-14 10:50   参与评论

在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

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (1006人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号