一jsp向servlet传值
jsp——〉servlet
*
1通过form表单
jsp传递
<form action="servlet" method="post">
<input type="text" name="user">
<input type="submit" value="提交">
</form>
servlet接收
String message = request.getParameter("user")
2、通过url
jsp传递
<!-- 自动获取项目名称 -->
<a href="${pageContext.request.contextPath}/MyServlet?name=xiaoming&password=1234">走</a>
或(不能传递中文字符)
<!-- 没有自动获取项目名称 -->
<a href="MyServlet?name=xiaoming&password=1234&age=18&sex=2">走ni</a>
servlet接收
String name1 = request.getParameter("name");
String password = request.getParameter("password");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
二、servlet向jsp传值
1、重定向
servlet传递
String user="xiaomimg";
request.setAttribute("user", user);
response.sendRedirect("index.jsp");
jsp接收
${user }
2、转发
servlet传递
String user="xiaomimg";
request.setAttribute("user", user);
request.getRequestDispatcher("index.jsp").forward(request, response);
jsp接收
${user }