Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)

        上了一个学期的Web结课了,这是最后一次作业,有的地方写的不是那么完善,也是在前面作业基础上做的,针对性不是很强但很全面。从html、css、js、servlet、连接mysql、jsp、javabean、jsp,再到最后的过滤器。一开始只是为了达到作业要求,后续也就懒得改了。

        这个项目可以实现登录功能、注册功能、显示专家信息、修改信息、修改密码、注销账号和退出。项目结构如下,使用了Servlet、JavaBean、Filter.        

Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第1张图片

web.xml配置如下:



  exp10
   
  	charSet
  	UTF-8
  
   
  	login
  	exp08.servlet.LoginServlet
  
  
 		login
 		/LoginServlet
  
   
  	register
  	exp08.servlet.RegisterServlet
  
  
 		register
 		/RegisterServlet
  
   
  	expert
  	exp08.servlet.ShowExpert
  
  
 		expert
 		/ShowExpert
  
   
  	update
  	exp08.servlet.UpdateServlet
  
  
 		update
 		/UpdateServlet
  
  
  	update1
  	exp08.servlet.UpdateServlet1
  
  
 		update1
 		/UpdateServlet1
  
  
  	quit
  	exp08.servlet.QuitServlet
  
  
 		quit
 		/QuitServlet
  
  
  	logout
  	exp08.servlet.LogoutServlet
  
  
 		logout
 		/LogoutServlet
  
  
  	Updatepwd
  	exp08.servlet.UpdatepwdServlet
  
  
 		Updatepwd
 		/UpdatepwdServlet
  
  
  	VerificationServlet
  	exp08.servlet.VerificationServlet
  
  
 		VerificationServlet
 		/VerificationServlet
  
  
  
   
  	encode
  	exp10.filter.EncodingFilter
  
  
  	encode
  	/*
  	REQUEST
	FORWARD
	INCLUDE
	ERROR	
  
  
  	LoginFilter
  	exp10.filter.LoginFilter
  
  
  	LoginFilter
  	/ucenter/*
  	REQUEST
	FORWARD
	INCLUDE
  
  
  	PwdFilter
  	exp10.filter.PwdFilter
  
  
  	PwdFilter
  	/UpdatepwdServlet
  	REQUEST
	FORWARD
	INCLUDE
  


  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
    
  	
  		encodingConfiguration
  		html encoding
  		*.html
  		UTF-8
  	

 mysql数据库为his,里面有两个表expert和user

 Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第2张图片

 Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第3张图片

Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第4张图片

 一、登录功能

首先是首页index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





HIS




<%@ include file="inc/header.html"%>
<%@ include file="inc/top.html"%>
<%@ include file="inc/sidemenu.html"%>
<%@ include file="inc/content.html"%>

jsp读取的文件太多,全放上来比较麻烦,这里放之前做的html文件,图片可以自己去找。

css文件放到最下面

index.html





首页





Tulip
English
欢迎使用HIS系统(web版) [  登   录  ] [  注   册  ]

实现了医院门诊部挂号处所需的各种功能,包括门诊安排的管理,号表的生成及维护,门诊预约管理和挂号处理,同时提供了病人信息的查询和有关挂号工作的统计功能。支持预约、限量、不限量、分时挂号。(功能主要包括:门诊安排、挂号处理、统计与查询等。)

本系统集划价收费功能于一体,费别及收费系数的自定义能力,灵活多样的输入方法,简单易学,允许项目在价表中不存在时手工划价,与门诊药房的库存关联,无药报警,集中统一的价表管理,支持医院"一卡通",集成医疗保险收费项目控制,费用自动分比例,费用按医疗保险政策分段统计等。

门诊药房管理系统是医院门诊处方药品的发放中心。可以根据药房的不同类别分:中药房、西药房、中成药等不同药房。药房与药库连用,直接从药库出库转药房入库,与门诊收费连接直接显示划价处方药品列表。可对患者处方查询;可对任意时间段的发药量查询;可对午间时间段的发药患者查询。

his

登录页面如下,注意把上面的登录跳转链接改一下。

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





Insert title here




<%@ include file="inc/header.html"%>
<%@ include file="inc/top.html"%>
<%if(session.getAttribute("msg")!=null){ out.println("

"+(String)session.getAttribute("msg")+"

"); } %>
register
用 户:
密 码:

然后是登录功能的实现

LoginServlet.java

package exp08.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;
import exp08.bean.UserBean;
import exp08.bean.UiBean;

public class LoginServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		String name = req.getParameter("uname");
		String pwd = req.getParameter("upwd");
		String[] param = new String[2];
		param[0] = name;
		param[1] = pwd;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String sql = "select * from user where username = ? and password = ?";
		
		try {
			conn = DBBean.getConn();
			rs = DBBean.getRs(sql, param, conn);
			if(rs.next()) {
				UiBean ui=new UiBean();
				UserBean ubean = new UserBean();
				ui.setlState(true);
				ui.setSid(session.getId());
				ubean.setId(rs.getString("id"));
				ubean.setUsername(rs.getString("username"));
				ubean.setPassword(rs.getString("password"));
				ubean.setRealname(rs.getString("realName"));
				ubean.setSex(rs.getString("sex"));
				ubean.setTelcode(rs.getString("telcode"));
				ubean.setAddress(rs.getString("address"));
				ubean.setCdh(rs.getString("cdh"));
				session.setAttribute("ui", ui);
				session.setAttribute("ubean", ubean);
				if(session.getAttribute("msg")!=null) 
					session.removeAttribute("msg");
				req.getRequestDispatcher("ucenter/uinfo.jsp").forward(req, resp);
				return;
			}else {
				session.setAttribute("msg", "用户名或密码错误,请重新登录");
				if(session.getAttribute("ui")!=null) session.removeAttribute("ui");
				if(session.getAttribute("ubean")!=null) session.removeAttribute("ubean");
				resp.sendRedirect("login.jsp");
				return;
			}			
		}catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBBean.close(rs, pst, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

	
}

二、注册功能

注册页面

register.html





Insert title here





search
欢迎使用HIS系统(web版) [  登   录  ] [  注   册  ]

用户注册


用户名:
设置密码:
  密码由6-10个字符组成 
确认密码:
真实姓名:
您的性别:
您的手机:
您的住址:
是否有慢病: 高血压 糖尿病 冠心病
 

然后是注册功能的实现

package exp08.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;

public class RegisterServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		String name = req.getParameter("userName");
		String[] param = new String[1];
		param[0] = name;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String sql = "select * from user where username = ?";
		
		try {
			conn = DBBean.getConn();
			rs = DBBean.getRs(sql, param, conn);
			if(rs.next()) {
				session.setAttribute("msg", "注册失败,该用户已存在");
				if(session.getAttribute("ubean")!=null) session.removeAttribute("ubean");
					resp.sendRedirect("login.jsp");
				return;
			}else {
				rs.close();
				sql="insert into user (username,password,realname,sex,telcode,address,cdh) value(?,?,?,?,?,?,?)";
				String[] param1 = new String[7];
				param1[0] = req.getParameter("userName");
				param1[1] = req.getParameter("userPwd");
				param1[2] = req.getParameter("realName");
				param1[3] = req.getParameter("sex");
				param1[4] = req.getParameter("mobile");
				param1[5] = req.getParameter("company");
				param1[6] = req.getParameter("mbs");
				conn = DBBean.getConn();
				DBBean.getRss(sql, param1, conn);
				session.setAttribute("msg", "注册成功!");
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}			
		}catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBBean.close(rs, pst, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

	
}

        特别注意,登录和注册都是要和数据库连接的,我用的是mysql,连接代码就在servlet里面,注意改一下数据库的名字、用户名和密码,而且还用了JavaBean,也就是连接数据库的时候调用的是javabean里的方法。数据库里的表的信息要和代码里的对应,这里把后面修改密码、显示专家信息的代码都一起放下面:

DBBean.java

package exp08.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBBean {
	private final static String DRIVER = "com.mysql.cj.jdbc.Driver";
	private final static String URL = "jdbc:mysql://localhost:3306/his?userSSL=false&serverTimezone=Asia/Shanghai";
	private final static String USER = "root";
	private final static String PASSWORD = "12345678";
	
	public static Connection getConn() throws ClassNotFoundException, SQLException {
		Class.forName(DRIVER);
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		return conn;
	}
	
	public static ResultSet getRs(String sql,String[] param,Connection conn) throws SQLException {
		PreparedStatement pst = conn.prepareStatement(sql);
		if(param!=null) {
			for(int i=1;i<=param.length;i++) {
				pst.setString(i, param[i-1]);
			}
		}
		ResultSet rs = pst.executeQuery();
		return rs;
	}
	public static void getRss(String sql,String[] param1,Connection conn) throws SQLException {
		PreparedStatement pst = conn.prepareStatement(sql);
		if(param1!=null) {
			for(int i=1;i<=param1.length;i++) {
				pst.setString(i, param1[i-1]);
			}
		}
		pst.executeUpdate();
		return;
	}
	
	public static void close(ResultSet rs,PreparedStatement pst,Connection conn) throws SQLException {
		if(rs!=null)rs.close();
		if(pst!=null)pst.close();
		if(conn!=null)conn.close();
	}
}

UseBean.java

package exp08.bean;

public class UserBean {
	private String id;
	private String username;
	private String password;
	private String realname;
	private String sex;
	private String telcode;
	private String address;
	private String cdh;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String name) {
		this.username = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getTelcode() {
		return telcode;
	}
	public void setTelcode(String telcode) {
		this.telcode = telcode;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCdh() {
		return cdh;
	}
	public void setCdh(String cdh) {
		this.cdh = cdh;
	}
	
}

PwdBean.java

package exp08.bean;

public class PwdBean {
	private String password;
	private String newpwd;
	private String renewpwd;
	
	public String getNewpwd() {
		return newpwd;
	}
	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}
	public String getRenewpwd() {
		return renewpwd;
	}
	public void setRenewpwd(String renewpwd) {
		this.renewpwd = renewpwd;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

ExpertBean.java

package exp08.bean;

public class ExpertBean {
	private String id;
	private String name;
	private String department;
	private String eclass;
	private String info;
	private String picture;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public String getEclass() {
		return eclass;
	}
	public void setEclass(String eclass) {
		this.eclass = eclass;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}		
	
}

UiBean.java

package exp08.bean;

public class UiBean {
	private String sid;
	private boolean lState;
	public String getSid() {
		return sid;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public boolean islState() {
		return lState;
	}
	public void setlState(boolean lState) {
		this.lState = lState;
	}
	
	
}

三、显示专家介绍页面

点击index.html的专家介绍即可跳转到专家介绍页面

这里只是在读取数据库中的专家信息并保存,转发到listexp.jsp中显示

ShowExpert.java

package exp08.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import exp08.bean.DBBean;
import exp08.bean.ExpertBean;

/**
 * Servlet implementation class ShowExpServlet
 */

public class ShowExpert extends HttpServlet {
	private static final long serialVersionUID = 1L;      
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String did = request.getParameter("did");
		
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String sql = "";
		try {
			conn = DBBean.getConn();
			if(did==null) {
				sql = "select * from expert";
				rs = DBBean.getRs(sql,null, conn);
			}else {
				sql = "select * from expert where left(id,1) = ?";
				String[] param = new String[1];
				param[0] = did;
				rs = DBBean.getRs(sql, param, conn);
			}			
			ArrayList ebeans = new ArrayList();
			while(rs.next()) {
				ExpertBean ebean = new ExpertBean();
	        	ebean.setId(rs.getString("id"));
	        	ebean.setName(rs.getString("name"));
	        	ebean.setDepartment(rs.getString("department"));
	        	ebean.setEclass(rs.getString("eclass"));
	        	ebean.setInfo(rs.getString("info"));
	        	ebean.setPicture(rs.getString("picture"));
	        	ebeans.add(ebean);
			}
	        request.setAttribute("ebeans", ebeans);
	        request.getRequestDispatcher("listexp.jsp").forward(request, response);
	        return;
	        	        
		}catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBBean.close(rs, pst, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

listexp.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList,exp08.bean.ExpertBean"%>





HIS





<%@ include file="inc/header.html"%>

专家信息列表

<% ArrayList ebeans = new ArrayList(); if(request.getAttribute("ebeans")!=null) ebeans = (ArrayList)request.getAttribute("ebeans"); for(ExpertBean ebean:ebeans){ out.println(""); } %>
编号姓名科室职称简介照片
"+ebean.getId()+""+ebean.getName()+""+ebean.getDepartment()+""+ebean.getEclass()+""+ebean.getInfo()+"

结果如下,点击姓名即可进入专家个人信息页面 

Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第5张图片

 四、信息操作功能(修改信息、注销账号、退出)

登录成功后显示个人信息界面,如下

Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第6张图片

uinfo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="exp08.bean.UserBean" %>
<%
UserBean ubean = null;
if(session.getAttribute("ubean")!=null){
	ubean = (UserBean)session.getAttribute("ubean");
}
%>





HIS





<%@ include file="/inc/header.html"%>

<%=ubean.getUsername()%>,欢迎光临个人中心   修改信息   修改密码   注销账户   退出登录   

用户个人信息


用户名:<%=ubean.getUsername() %>
密码:<%=ubean.getPassword() %>
真实姓名:<%=ubean.getRealname() %>
性别:<%=ubean.getSex() %>
手机号码:<%=ubean.getTelcode() %>
家庭地址:<%=ubean.getAddress() %>
慢病史:<%=ubean.getCdh() %>

 点击修改信息,进入edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>






HIS





<%@ include file="../inc/header.html"%>

,欢迎光临个人中心   修改信息   修改密码   注销账户   退出登录   

修改信息


用户名:<%=ubean.getUsername() %>
密码:<%=ubean.getPassword() %>
真实姓名:<%=ubean.getRealname() %>
性别:<%=ubean.getSex() %>
手机号码:>
家庭地址:>
慢病史: 高血压 糖尿病 冠心病

页面如下Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第7张图片

确认后提交到UpdateServlet

package exp08.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;
import exp08.bean.UserBean;

public class UpdateServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		HttpSession session=request.getSession();
		PrintWriter out=response.getWriter();
		UserBean ubean = null;
		if(session.getAttribute("ubean")!=null){
			ubean = (UserBean)session.getAttribute("ubean");
		}
		if(ubean.getId()==null) {
			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("/html"); out.flush(); out.close(); return;} else { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; String[] param1 = new String[4]; String cdh=""; param1[0] = request.getParameter("telcode"); param1[1] = request.getParameter("address"); if(request.getParameterValues("cdh")!=null) { String[] mbs=request.getParameterValues("cdh"); for(int i=0;i

 UpdateServlet只是修改了数据库,修改完后要重新显示个人信息需要把信息保存到session里在输出,又写了一个UpdateServlet1

package exp08.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;
import exp08.bean.UserBean;

public class UpdateServlet1 extends HttpServlet{

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		HttpSession session=request.getSession();
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		UserBean ubean = null;
		if(session.getAttribute("ubean")!=null){
			ubean = (UserBean)session.getAttribute("ubean");
		}
		String[] param = new String[1];
		param[0] = ubean.getId();
		String sql="select * from user where id = ?";
		
		try {
			conn = DBBean.getConn();
			rs=DBBean.getRs(sql, param, conn);
			if(rs.next()) {
			ubean.setId(rs.getString("id"));
			ubean.setUsername(rs.getString("username"));
			ubean.setPassword(rs.getString("password"));
			ubean.setRealname(rs.getString("realName"));
			ubean.setSex(rs.getString("sex"));
			ubean.setTelcode(rs.getString("telcode"));
			ubean.setAddress(rs.getString("address"));
			ubean.setCdh(rs.getString("cdh"));
			session.setAttribute("ubean", ubean);
			request.getRequestDispatcher("ucenter/uinfo.jsp").forward(request,response);
			return;
			}
		}catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBBean.close(rs, pst, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
		

到这里修改信息的操作实现了,接下来是修改密码。

pwd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>






HIS





<%@ include file="../inc/header.html"%>
<%if(session.getAttribute("msg")!=null){ out.println("

"+(String)session.getAttribute("msg")+"

"); } %>

<%=ubean.getUsername()%>,欢迎光临个人中心   修改信息   修改密码   注销账户   退出登录   

修改信息


原密码:<%=ubean.getPassword() %>
新密码:
确认密码:

点击修改密码,页面如下: 

Web应用开发课程--实验十过滤器作业(中文乱码过滤、登录过滤器和密码修改过滤器)_第8张图片

pwd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>






HIS





<%@ include file="../inc/header.html"%>
<%if(session.getAttribute("msg")!=null){ out.println("

"+(String)session.getAttribute("msg")+"

"); } %>

<%=ubean.getUsername()%>,欢迎光临个人中心   修改信息   修改密码   注销账户   退出登录   

修改信息


原密码:<%=ubean.getPassword() %>
新密码:
确认密码:

 修改完后用过滤器进行密码验证

VerificationServlet.java

package exp08.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;
import exp08.bean.UserBean;
import exp08.bean.PwdBean;

public class VerificationServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		HttpSession session=request.getSession();
		UserBean ubean = null;
		if(session.getAttribute("ubean")!=null){
			ubean = (UserBean)session.getAttribute("ubean");
		}
		if(ubean==null) {
			session.setAttribute("msg", "您还未登录,请先登录!");
			if(session.getAttribute("ui")!=null) session.removeAttribute("ui");
			if(session.getAttribute("ubean")!=null) session.removeAttribute("ubean");
			response.sendRedirect("login.jsp");
			return;}
		else {
		PwdBean pbean = new PwdBean();
		String newpwd = request.getParameter("newpwd");
		String renewpwd = request.getParameter("renewpwd");
		pbean.setNewpwd(newpwd);
		pbean.setRenewpwd(renewpwd);
		session.setAttribute("pbean", pbean);
		request.getRequestDispatcher("UpdatepwdServlet").forward(request,response);
		return;
		}
		}
}
		

UpdatepwdServlet.java

package exp08.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;
import exp08.bean.UserBean;
import exp08.bean.PwdBean;

public class UpdatepwdServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		HttpSession session=request.getSession();
		UserBean ubean = null;
		if(session.getAttribute("ubean")!=null){
			ubean = (UserBean)session.getAttribute("ubean");
		}
		if(ubean==null) {
			session.setAttribute("msg", "您还未登录,请先登录!");
			if(session.getAttribute("ui")!=null) session.removeAttribute("ui");
			if(session.getAttribute("ubean")!=null) session.removeAttribute("ubean");
			response.sendRedirect("login.jsp");
			return;}
		else {
		PwdBean pbean = (PwdBean)session.getAttribute("pbean"); 
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String newpwd = pbean.getNewpwd();
		String[] param1 = new String[2];
		param1[0] = newpwd;
		param1[1] = ubean.getId();
		String sql = "update user set password = ? where id = ?";
		
		try {
		conn = DBBean.getConn();
		DBBean.getRss(sql, param1, conn);
		session.setAttribute("pbean", pbean);
		request.getRequestDispatcher("UpdateServlet1").forward(request,response);
		return;
		}catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBBean.close(rs, pst, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
}}
		

之后即可跳转到个人信息页面,到这里修改密码操作完成。接下来是注销账户操作,就是把数据库里的用户信息删除。

QuitServlet.java

package exp08.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import exp08.bean.DBBean;
import exp08.bean.UserBean;

public class QuitServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
		UserBean ubean = null;
		if(session.getAttribute("ubean")!=null){
			ubean = (UserBean)session.getAttribute("ubean");
		}
		if(ubean==null) {
			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("/html"); out.flush(); out.close(); return; }else { Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; String[] param1 = new String[1]; param1[0]=ubean.getId(); String sql="delete from user where id = ?"; try { conn = DBBean.getConn(); DBBean.getRss(sql, param1, conn); request.getRequestDispatcher("LogoutServlet").forward(request,response); return; }catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }finally { try { DBBean.close(rs, pst, conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}

然后是退出操作

LogoutServlet.java

package exp08.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogoutServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
		request.getSession().invalidate();
		response.sendRedirect("index.jsp");
		return;
	}
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
		doGet(request,response);
	}
}

五、过滤器

分别有三个过滤器,分别是乱码过滤、登录过滤、密码验证过滤,登录过滤是为了防止用户不登录直接跳转到网页,密码验证过滤是为了使修改密码不为空且两次密码相同。

EncodingFilter.java

package  exp10.filter; 
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;


public class EncodingFilter implements Filter {
	private String charSet;
	public void init(FilterConfig config) throws ServletException {
		this.charSet=config.getServletContext().getInitParameter("charSet");
					}
	public void destroy() {}
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;		
		request.setCharacterEncoding(charSet);
		if (!((HttpServletRequest) request).getServletPath().contains("css")) {
			response.setContentType("text/html; charset=" + charSet);
		}
		chain.doFilter(request, response);
	}
} 

LoginFilter.java

package exp10.filter;

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 javax.servlet.http.HttpSession;

import exp08.bean.UiBean;

public class LoginFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;
		HttpSession session=request.getSession();
		UiBean ui=(UiBean)session.getAttribute("ui");
		if(ui!=null&&ui.islState()==true&&ui.getSid()==session.getId()){
			chain.doFilter(req, res);
		}else{
			if(session.getAttribute("msg")==null) session.setAttribute("msg", "你还没有登录,请登录后再访问用户中心");
			if(session.getAttribute("ui")!=null) session.removeAttribute("ui");
			if(session.getAttribute("ubean")!=null) session.removeAttribute("ubean");
			response.sendRedirect(req.getServletContext().getContextPath()+"/login.jsp");
			return;
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}

PwdFilter.java

package exp10.filter;

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 javax.servlet.http.HttpSession;

import exp08.bean.PwdBean;
import exp08.bean.UiBean;
import exp08.servlet.VerificationServlet;

public class PwdFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;
		HttpSession session=request.getSession();
		PwdBean pbean=(PwdBean)session.getAttribute("pbean");
		UiBean ui=(UiBean)session.getAttribute("ui");
		if(!(pbean.getNewpwd().equals(""))&&pbean.getNewpwd().equals(pbean.getRenewpwd())&&ui.getSid()==session.getId()){
			chain.doFilter(req, res);
		}else{
			session.setAttribute("msg", "两次输入的密码不相同,请重新输入!");
			response.sendRedirect(req.getServletContext().getContextPath()+"/ucenter/pwd.jsp");
			return;
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}

六、css

style1.css

@charset "UTF-8";
/*IE浏览器默认的盒子属性值。*/
* {
  box-sizing: border-box;
}

body {
  margin: 0;
}
.header {
  text-align: left;
  padding: 2px;
}
/*搜索框*/
.header input {
	position: relative;
    left: 250px;
    bottom: 20px;
}
/*放大镜图片*/
.search {
	position: absolute;
	left: 1050px;
	top: 45px;
}

/* “英文版”图片 */
.English {
	position: absolute;
	left: 1200px;
	top: 50px;
}
.topmenu {
  color: white;
  background-color: #777;
  font-size: 25px;
}

.topmenu img {
	width:45px;
		height:39px;
		vertical-align:middle;
		border: solid thin #777;
		}
.topmenu table{text-align: center;
			width:100%;}
/*以下四句是实验三所做修改*/
.topmenu table td.l {
	width: 20%;
				}
.topmenu table td.m {
	width:60%;
	}
	
.topmenu table td.r {
	width:20%;
	background-color: white;
	}
	
#time  {
  	font-weight:bolder;
  	font-size:x-large;
		}
		
.topmenu table a {
	vertical-align:middle;
	font-size:14px;	
	text-decoration: none;}

.sidemenu {
  display:block;
  word-break:keep-all;/* 不换行 */
  white-space:nowrap;/* 不换行 */
  width: 25%;
  
}

/*消除列表前面的点*/
.sidemenu ul {
  list-style-type: none;
  text-align:center;
}

.sidemenu ul li {
	position: relative;
	background-color:gray;
}

/*刷新自己显示标题*/
.sidemenu ul li ul {
	position:absolute;
	left:145px;
	margin-top:-41px;
	margin-left: 150px;
	width:150px;	
	display:none;	
}

.sidemenu ul li ul li {margin:0;}
.sidemenu ul li ul li a {
	background-color: #f7f7f7;
	border-left: #dfdfdf 2px solid;
	border-right: #dfdfdf 2px solid;
}

/*跳转按钮属性(这里把a标签设置成块元素,有宽、高和内外边距)*/
.sidemenu li a {
  margin-bottom: 4px;
  display: block;
  padding: 8px;
  background-color: #eee;
  text-decoration: none;
  color: #666;
}

/*鼠标放在a标签上会发上变化*/
.sidemenu li a:hover {
  background-color: #555;
  color: white;
}


/*消除漂浮带来的位置影响*/
.clearfix::after {
  content: "";
  clear: both;
  display: table;
}
/*派生选择器控制目录+文字,使其漂浮*/
.column {
  float: left;
  padding: 5px;
}
/*最底部版权栏*/
.footer {
  background-color: #444;
  color: white;
  text-align:center;
  padding: 15px;
}

main.css

@charset "UTF-8";
* {
  box-sizing: border-box;
}

body {
  text-align:center;
  margin: 0;
}

.header {
  background-color: white;
  background-image:url("../images/logo.jpg");
  background-repeat:no-repeat;
  text-align: right;
  height: 68px;
}

.footer {
  background-color: #444;
  color: white;
  padding: 10px;
  text-align: center;
}
.footer p {
  text-align: center;
  font-size:14px;}
.top {
  color: white;
  background-color: #777;
  font-size: 25px;
}

.top img {width:45px;
		height:39px;
		vertical-align:middle;
		border: solid thin #777;
		}
.top table{text-align: center;
			width:100%;}
/*以下四句是实验三所做修改*/
.top table td.l {width: 20%;
				background-color: white;}
.top table td.m {width:60%;}
.top table td.r {
	width:20%;
	background-color: white;}
#time  {font-size:14px;
		color:black;}
.top table a {
	vertical-align:middle;
	font-size:14px;	
	text-decoration: none;}

.column {
  float: left;
  padding: 15px;  
}

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

.sidemenu {
  display:block;
  word-break:keep-all;/* 不换行 */
  white-space:nowrap;/* 不换行 */
  width: 15%;
}

.content {
  width: 70%;
}
.content img{
	width:400px;
	height:300px;
	float:right;
	margin:0 10px 10px 10px;
}

.content p{
	width:80%;
	text-indent: 2em;	
}
.sidemenu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;  
  width:150px;
}

.sidemenu ul li {
	position: relative;
}

.sidemenu ul li ul {
	position: absolute;
	margin-top:-41px;
	margin-left: 150px;
	width:150px;
	display:none;		
}

.sidemenu ul li ul li {margin:0;}
.sidemenu ul li ul li a {
	background-color: #f7f7f7;
	border-left: #dfdfdf 2px solid;
	border-right: #dfdfdf 2px solid;
}
.sidemenu li a {
  margin-bottom: 4px;
  display: block;
  padding: 8px;
  text-decoration: none;
  color:black;
  
}

.sidemenu li a:hover {
  color:red;
}


#sdiv {
	width:500px;
	height:30px;
	position: absolute;
	top:19px;
	left:700px; 
	}

#search {width:30px;
		height:30px;
		vertical-align:middle;}
		
#sbutton {		
		height:30px;
		margin-left: 50px;
		vertical-align:middle;}
		
#sinput {height:30px;
		vertical-align:middle;
		}

one.css

@charset "UTF-8";


.content {
	margin:0 auto;
	width:80%;
}
.content table{
	border-collapse: collapse;
	text-align:center;	
}
.content table tr {
	border-bottom: solid thin black;
}
.content table img {
	width:70px;
	height:90px;
}
.content table tr td.d1{
	width:10%;
}
.content table tr td.d2{
	width:50%;
}
.content table tr td.info{
	text-align:left;
	text-indent:2em;
}

reg.css

@charset "UTF-8";
/*注册*/
.content {margin:0 auto;width:70%;
		text-align:center;	
		padding: 20px 0;}
.content div {
		border:solid thin red;
		margin:0 auto;
		padding:5px;	}
.reg {
	font-size: 16px;
	line-height: 35px;
	text-align:left;
}
.reg input[type="text"], .reg input[type="password"] {
	border: 1px solid #ccc;
	padding: 5px;
	color: #999;
}
.reg select {
	padding: 5px;
	height: 30px;
	margin-right: 5px;
	color: #999;
}
.reg textarea {
	color: #999;
}
.gray12 {
	font-size: 12px;
	color: #999;
}
.bian {
	margin-top: 10px;
}
.bian:hover {
	filter: alpha(opacity=50); /*IE滤镜,透明度50%*/
	-moz-opacity: 0.5; /*Firefox私有,透明度50%*/
	opacity: 0.5;/*其他,透明度50%*/
}

你可能感兴趣的:(servlet,java,eclipse,tomcat,mysql)