我有一个jsp页面" one.jsp"它有一个按钮。
点击我要拨打的按钮" two.jsp"具有相同的请求对象。我尝试了几种选择: -
1)点击按钮,将请求发送到servlet,servlet将请求转发给" two.jsp"在请求调度程序的帮助下,但无法使用请求对象参数" one.jsp"使用。
2)建立了活动链接并使用了response.sendredirect(),但创建了一个新请求
我在互联网上搜索了<jsp:forward page="two.jsp">
,但我不知道如何在点击按钮时进行jsp前进。
答案 0 :(得分:1)
one.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="servletToJsp">
<input type="submit"></button>
</form>
</body>
</html>
two.jsp
<html>
<body bgcolor="white">
<h1> I have been invoked by
<%
out.print(request.getAttribute("servletName").toString());
%>
Servlet
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WebPrj</display-name>
<servlet>
<description></description>
<display-name>servletToJsp</display-name>
<servlet-name>servletToJsp</servlet-name>
<servlet-class>com.servletToJsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletToJsp</servlet-name>
<url-pattern>/servletToJsp</url-pattern>
</servlet-mapping>
</web-app>
com包中的servletToJsp.java
package com;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class servletToJsp extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
request.setAttribute("servletName", "servletToJsp");
try {
getServletConfig().getServletContext().getRequestDispatcher(
"/two.jsp").forward(request,response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}