1.jsp跳转jsp
jsp1代码
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: 49841
Date: 2020/9/7
Time: 21:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax</title>
</head>
<body>
<form action="index.jsp">
用户名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
<%--<%
String username = request.getParameter("username");
System.out.println("在登陆界面得到的username值:" username);
request.getSession().setAttribute("name", username);
%>--%>
</body>
</html>
jsp2代码
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: 49841
Date: 2020/9/7
Time: 10:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%--<%
/*String username1 = request.getParameter("username");
System.out.println("在index界面得到的username值:" username1);*/
String username = request.getParameter("username");
request.setAttribute("username", username);
/*request.getSession().setAttribute("username", username1);
System.out.println("session中的username值:" request.getSession().getAttribute("username"));*/
%>--%>
<h2>欢迎来到主页面,呱?!</h2>
<span>显示用户名getSession:</span> <span>${param.username}</span> <br/>
<%-- <span>显示用户名getSession:</span> <span>${servletname}</span> <br/>--%>
<%-- <span>显示用户名getParameter:</span><span>${username1}</span> <br/>--%>
<%-- <span>显示用户名getParameter:</span><span>username1</span> <br/>--%>
</body>
</html>
1.这种使用的是${param.username}获取第一个界面传来的数据。
2.也可以在上面先得到前一个jsp页面传来的参数,再讲参数放到request或者其他域中, 然后使用${username}得到
代码语言:javascript复制 String username = request.getParameter("username");
request.setAttribute("username", username);
代码语言:javascript复制 <span>显示用户名getSession:</span> <span>${param.username}</span> <br/>
2.jsp跳转servlet跳转jsp
jsp1页面代码
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: 49841
Date: 2020/9/7
Time: 21:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax</title>
</head>
<body>
<form action="TestServlet1">
用户名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
<%--<%
String username = request.getParameter("username");
System.out.println("在登陆界面得到的username值:" username);
request.getSession().setAttribute("name", username);
%>--%>
</body>
</html>
servlet代码
代码语言:javascript复制package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* zt
* 2020/9/7
* 22:17
*/
@WebServlet(value = "/TestServlet1")
public class TestServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String jspname = request.getParameter("username");
System.out.println("在servlet中得到的值:" jspname);
// request.setAttribute("servletname", jspname);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
jsp2页面代码
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: 49841
Date: 2020/9/7
Time: 10:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%--<%
/*String username1 = request.getParameter("username");
System.out.println("在index界面得到的username值:" username1);*/
String username = request.getParameter("username");
request.setAttribute("username", username);
/*request.getSession().setAttribute("username", username1);
System.out.println("session中的username值:" request.getSession().getAttribute("username"));*/
%>--%>
<h2>欢迎来到主页面,呱?!</h2>
<span>显示用户名getSession:</span> <span>${param.username}</span> <br/>
<%-- <span>显示用户名getSession:</span> <span>${servletname}</span> <br/>--%>
<%-- <span>显示用户名getParameter:</span><span>${username1}</span> <br/>--%>
<%-- <span>显示用户名getParameter:</span><span>username1</span> <br/>--%>
</body>
</html>
1.这种方法jsp2页面代码不变,还是使用${param.username}来得到传来的参数,它们三个互相传递的也都是参数 2.还可以在servlet中将得到的参数放在域中,然后在jsp2界面里从域中获取属性一样可以得到(不要将参数和属性搞混掉),参数是getParameter得到的,属性是getAttribute得到的。