1.使用jstl进行页面上的呈现结果集
//jsp文件
<body>
<form action="log.do" method="post">
<input type='submit' value='查询' />
</form>
</body>
//servlet文件
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<String>url=new ArrayList<>();
url.add("这个数据");
url.add("这2数据");
url.add("这3数据");
url.add("这4数据");
List<User>user=new ArrayList<>();
User u1=new User("第一个","哈哈");
User u2=new User("第2个","哈哈");
User u3=new User("第3个","哈哈");
User u4=new User("第4个","哈哈");
user.add(u1);
user.add(u2);
user.add(u3);
user.add(u4);
Map<Integer,User>map=new HashMap<>();
map.put(10, u1);
map.put(20, u2);
map.put(30, u3);
map.put(40, u4);
request.setAttribute("user", user);
request.setAttribute("url",url);
request.setAttribute("map", map);
RequestDispatcher ul=request.getRequestDispatcher("show.jsp");
ul.forward(request, response);
}
//jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<p>这是一个例子</p>
<c:forEach items="${requestScope.url}" var="str">
<p>${str} </p>
</c:forEach>
<table>
<tr>
<th>标题行1</th>
<th>标题行2</th>
</tr>
<c:forEach items="${requestScope.user }" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
</tr>
</c:forEach>
</table>
<table>
<tr>
<th>标题行1</th>
<th>标题行2</th>
</tr>
<c:forEach items="${requestScope.map }" var="entry">
<tr>
<td>${entry.key}</td>
<td>${entry.value.id}</td>
<td>${entry.value.name}</td>
</tr>
</c:forEach>>
</table>
</body>
</html>
2.监听器 java(接口) js(一类对象)
3.示例代码
//开始界面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action='login.do' method='post'>
<input type='text' name='uid' /><br>
<input type='password' name='pwd' /><br>
<input type='submit' value='登录' />
</form>
</body>
</html>
//开始servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String uid=request.getParameter("uid");
String pwd=request.getParameter("pwd");
RequestDispatcher rd=null;
if(uid!=null&&pwd!=null&&pwd.equalsIgnoreCase("123")){
User myuser=new User(uid,"曹操");
HttpSession hs=request.getSession();
hs.setAttribute("myuser", myuser);
rd=request.getRequestDispatcher("main.jsp");
rd.forward(request, response);
}else {
rd=request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
}
//退出界面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<p>欢迎登录,你的ID是${sessionScope.myuser.id}</p>
<a href='logout.do'>退出</a>
</body>
</html>
//退出servlet
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession hs=request.getSession();
hs.invalidate();
response.sendRedirect("login.jsp");
}
//监听器
package com.neusoft;
import java.util.Enumeration;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class SessionListener
*
*/
@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
/**
* Default constructor.
*/
public SessionListener() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent se) {
// TODO Auto-generated method stub
//建立
HttpSession hs=se.getSession();
System.out.println("会话对象已经建立 "+hs.getId());
}
/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent se) {
// TODO Auto-generated method stub
HttpSession hs=se.getSession();
System.out.println("会话对象已经销毁 "+hs.getId());
}
/**
* @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
*/
public void attributeAdded(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
HttpSession hs=se.getSession();
//得到所有键的集合
Enumeration<String> es=hs.getAttributeNames();
//boolean 类型 可以看到集合中是否还有键
while(es.hasMoreElements()) {
//得到键名
String key=es.nextElement();
System.out.println("session中的KEY"+key);
}
}
/**
* @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
*/
public void attributeRemoved(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
HttpSession hs=se.getSession();
try {//得到所有键的集合
Enumeration<String> es=hs.getAttributeNames();
//boolean 类型 可以看到集合中是否还有键
while(es.hasMoreElements()) {
//得到键名
String key=es.nextElement();
System.out.println("session中的KEY"+key);}
}catch(Exception e){
}finally {
System.out.println("session中的key已经删除了");
}
}
/**
* @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
*/
public void attributeReplaced(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
}
}