加入收藏 | 设为首页 | 会员中心 | 我要投稿 驾考网 (https://www.jiakaowang.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

在 JSP 中使用 URL 重定向来传输参数的技巧

发布时间:2023-11-03 10:46:19 所属栏目:教程 来源:
导读:本文讲解了在servlet页中重定向到index.jsp页,但重写index.jsp页的URL传递两个参数给index页。

1.Servlet页中的内容:

/**
*
* @comefrom cuoxin.com

*/

package MyServlet;

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

1.Servlet页中的内容:

/**
*
* @comefrom cuoxin.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
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"

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错新网CUOXIN</title>
</head>
<body>
<%
String name = request.getParameter("name");
String a = request.getParameter("sid");
out.print(a + "<br>" + name);
%>
</body>
</html>

3.输出结果:

5748

hell

(编辑:驾考网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章