首 页IT知识库翔宇问吧收藏本站
当前位置:翔宇亭IT乐园IT知识库WEBJSP

JSP中利用URL重写传递参数的方法

减小字体 增大字体 作者:不详  来源:转载  发布时间:2010-10-14 18:26:30

本文讲解了在servlet页中重定向到index.jsp页,但重写index.jsp页的URL传递两个参数给index页。

1.Servlet页中的内容:

/**
*
* @comefrom biye5u.com

*/

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>翔宇亭IT乐园</title>
</head>
<body>
<%
String name = request.getParameter("name");
String a = request.getParameter("sid");
out.print(a + "<br>" + name);
%>
</body>
</html>

3.输出结果:

5748

hell
 


本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

知识评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 1分

内 容:

            请注意用语文明且合法,不要发布带有攻击性、侮辱性的言论,谢谢合作!

         通知管理员 验证码:

关于本站 | 网站帮助 | 广告合作 | 网站声明 | 友情连接 | 网站地图 | 用户守则 | 联系我们 |
本站大多数内容来自互联网或网站会员发布,如有侵权,请来信告之,谢谢!
Copyright © 2007-2017 biye5u.com. All Rights Reserved.
网站备案号:黑ICP备13005378号-3