TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
本文主要描述Servlet当中如何向前台jsp页面传值,本例传值部分分为三个部分,1 传入普通字符串 ;2 传入普通List; 3 传入带有多个Map的list,页面对传入的值通过jstl和el表达式来进行处理。 1 开始页面jspservletjstl.jsp <%@ page contentType="text/HTML;charset=UTF-8" isErrorPage="true" %>
<html>
<head>
<title></title>
</head>
<body>
<FORM action="gojstl" method="post">
<input type="text" name="username">
<input type="submit">
</FORM>
</body>
</html> 2 web.xml配置 <!-- jsp与jstl测试 -->
<servlet>
<servlet-name>gojstl</servlet-name>
<servlet-class>second.Gojstl</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>gojstl</servlet-name>
<url-pattern>/gojstl</url-pattern>
</servlet-mapping> 3 servlet类的处理 package second; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Gojstl extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
request.setAttribute("username",username);
Map map1 = new HashMap();
map1.put("username",username+1);
map1.put("idcard","111111111111111111");
Map map2 = new HashMap();
map2.put("username",username+2);
map2.put("idcard","2222222222222222222");
List list1 = new ArrayList();
list1.add(map1);
list1.add(map2);
List list2 = new ArrayList();
list2.add("map1");
list2.add("map2");
request.setAttribute("pagelist1",list1);
request.setAttribute("pagelist2",list2);
this.getServletContext().getRequestDispatcher("/showresult.jsp")
.forward(request, response);
//response.sendRedirect("finish.jsp");
}
}
4 显示处理页面showresult.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>jstl的jsp写法后台传值</title>
</head>
<body> <c:if test="false">
判断成功<br>
</c:if> <c:forEach var="bl" items="">
<br>
</c:forEach> <c:forEach var="b2" items="">
<br>
</c:forEach> </body>
</html>
源码下载:http://file.javaxxz.com/2014/10/2/100108797.zip |
|