servlet向jsp传值 为什么得到的总是NULL,请大家帮帮我这个新手。 这是myservlet.java
这是myservlet.javaimportjava.io.IOException;importjava.io.PrintWriter;importjavax.servl...
这是myservlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setAttribute("name", "孙悟空");
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
}
这是MyJsp.jsp(就贴了BODY里面的内容)
<body>
<%
String name=request.getParameter("name");
%>
This is my JSP page.<%=name%> <br>
</body> 展开
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setAttribute("name", "孙悟空");
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
}
这是MyJsp.jsp(就贴了BODY里面的内容)
<body>
<%
String name=request.getParameter("name");
%>
This is my JSP page.<%=name%> <br>
</body> 展开
展开全部
String name=request.getParameter("name");
改为String name=request.getAttribute("name");
因为你用的是request.setAttribute("name", "孙悟空");
request.getParameter("")得到是连接后面跟的参数
比如你这样request.getRequestDispatcher("MyJsp.jsp?name=123").forward(request, response);写。。然后jsp里面request.getParameter("name")就可以得到123了。
改为String name=request.getAttribute("name");
因为你用的是request.setAttribute("name", "孙悟空");
request.getParameter("")得到是连接后面跟的参数
比如你这样request.getRequestDispatcher("MyJsp.jsp?name=123").forward(request, response);写。。然后jsp里面request.getParameter("name")就可以得到123了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询