内容发布更新时间 : 2024/12/23 8:20:26星期一 下面是文章的全部内容请认真阅读。
Java web应用编程基础实验报告
实验5:Servlet实验
一、实验目的:
? 理解Servlet、过滤器和监听器概念,掌握Servlet、过滤器和监听器的基本使用方法。
二、实验要求:
? 使用专业开发工具MyEclipse编程。
? 上交实验报告电子文档(文档包含源程序,以班级、学号、姓名依次出现组成的
字符串标识文档,以班级为单位上交)。
三、实验内容:
1、用JSP编写用户登录页面,用Servlet和处理并在返回给客户端的页面上,显示登录时填写的用户名及密码。
2、用JSP编写发帖页面,用过滤器过滤帖子中的非法文字如脏话、下流词语(过滤掉的每个字符均用一个“*”代替),将过滤后的帖子内容在另一显示页面上输出。
3、应用监听器监听某个用户的登录。当该用户登录时,在另一监控页面(假定是系统管理员的监控页面)上会出现相应提示信息。
四、实验过程中遇到的问题及解决手段: 在实验2的时候,过滤器的配置文件总是写错。导致文件无法识别或无效,通过网上查找解决了问题。
五、程序源代码:
1. SessionServlet.java web.xml
SessionServlet.java
package com;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.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 javax.servlet.http.HttpSession; /**
* 使用HttpSession管理会话的登录Servlet */
@WebServlet(\)
public class SessionServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet() */
public SessionServlet() { super();
// TODO Auto-generated constructor stub }
/**
* @see HttpServlet#doPost(HttpServletRequest request, */
protected void doPost(HttpServletRequest request, HttpServletResponse
response.setContentType(\); PrintWriter out = response.getWriter(); /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse */
protected void doGet(HttpServletRequest request, HttpServletResponse }
doPost(request, response);
response)
response) throws ServletException, IOException {
HttpServletResponse response)
response) throws ServletException, IOException {
out.println(\); out.println(\); out.println(\);
out.println(\使用HttpSession管理会话的登录页面\); out.println(\); out.println(\);
//获取会话对象
HttpSession session = request.getSession(); //从会话对象中读取数据
Boolean isLogin = (Boolean)session.getAttribute(\);
if(isLogin == null){ }
String user = request.getParameter(\); String password = request.getParameter(\);
if(isLogin.booleanValue()){
//会话对象中读取数据
user = (String)session.getAttribute(user);
password = (String)session.getAttribute(password);
out.println(\欢迎您,\+user+\);
out.println(\您的登录密码是:\+password+\); //会话对象中保存数据
session.setAttribute(\, user);
session.setAttribute(\, Boolean.TRUE); session.setAttribute(\, password);
out.println(\欢迎您,\+user+\);
out.println(\您的登录密码是:\+password+\); out.println(\请在下面输入登录信息\); out.println(\); out.println(\);
out.println(\);
out.println(\用户名:\);
out.println(\); out.println(\);
isLogin = Boolean.FALSE;
}else if((user != null)&&(password != null)){
}else{
}
}
}
out.println(\);
out.println(\密码:\);
out.println(\); out.println(\);
out.println(\); out.println(\);
out.println(\确定out.println(\重置out.println(\);
out.println(\); out.println(\);
\\\); \\\);
out.println(\); out.println(\);
web.xml
xmlns:web=\ xsi:schemaLocation=\ http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\ id=\ version=\>
2. CheckFormFilter.java Encoding.java CheckForm.jsp CheckFormOver.jsp CheckFormFilter.java
package com;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import jdk.nashorn.internal.ir.RuntimeNode.Request;
public class CheckFormFilter implements Filter { protected FilterConfig config; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse)response;
web.xml