A.jsp页面传参至B.jsp页面
1.目录结构
2.A.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>A.JSP</title>
</head>
<body>
<form action="/jspToJsp/AServlet">
<h1>form表单传参</h1>
<label>姓名:</label>
<input name="valueA" type="text" value="JOY">
<input type="submit" value="提交">
</form>
<h2>a标签传值</h2>
<a href="/jspToJsp/AServlet?valueB='ANNE'">a标签传值:通过href中?后面的参数进行传值</a>
</body>
</html>
3.AServlet.java
package a;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AServlet")
public class AServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
String a =request.getParameter("valueA");
String b =request.getParameter("valueB");
request.setAttribute("a", a);
request.setAttribute("b", b);
request.getRequestDispatcher("/B.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4.B.jsp
<%@page import="javafx.scene.control.Alert"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>标题</title>
</head>
<body>
<h1>表单传参值为:${a}</h1>
<h1>a标签传参值为:${b}</h1>
</body>
</html>
5.注意事项
若出现<%@报错请自行添加server。如下图所示
若出现uri="http://java.sun.com/jsp/jstl/core"报错,可下载jstl-1.2.jar放到webContent的lib目录下。