上接javaweb版本的答答租车系统(源代码二)
这里是我的web.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CarSystem</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>登录</description>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.david.Servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>退出</description>
<servlet-name>ExitServlet</servlet-name>
<servlet-class>com.david.Servlet.ExitServlet</servlet-class>
</servlet>
<servlet>
<description>商品</description>
<servlet-name>CommodityServlet</servlet-name>
<servlet-class>com.david.Servlet.CommodityServlet</servlet-class>
</servlet>
<servlet>
<description>租车</description>
<servlet-name>RentCarServlet</servlet-name>
<servlet-class>com.david.Servlet.RentCarServlet</servlet-class>
</servlet>
<servlet>
<description>用户账单</description>
<servlet-name>UserBillServlet</servlet-name>
<servlet-class>com.david.Servlet.UserBillServlet</servlet-class>
</servlet>
<servlet>
<description>退租</description>
<servlet-name>SurrenderServlet</servlet-name>
<servlet-class>com.david.Servlet.SurrenderServlet</servlet-class>
</servlet>
<servlet>
<description>统计用户租车次数和总租金</description>
<servlet-name>SettleAccountsServlet</servlet-name>
<servlet-class>com.david.Servlet.SettleAccountsServlet</servlet-class>
</servlet>
<servlet>
<description>登记出租</description>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.david.Servlet.RegisterServlet</servlet-class>
</servlet>
<servlet>
<description>注册</description>
<servlet-name>RegServlet</servlet-name>
<servlet-class>com.david.Servlet.RegServlet</servlet-class>
</servlet>
<servlet>
<description>修改资料</description>
<servlet-name>ModifyServlet</servlet-name>
<servlet-class>com.david.Servlet.ModifyServlet</servlet-class>
</servlet>
<servlet>
<description>跳转登记租车页面</description>
<servlet-name>RegisterJumpServlet</servlet-name>
<servlet-class>com.david.Servlet.RegisterJumpServlet</servlet-class>
</servlet>
<servlet>
<description>跳转注册页面</description>
<servlet-name>RegJumpServlet</servlet-name>
<servlet-class>com.david.Servlet.RegJumpServlet</servlet-class>
</servlet>
<servlet>
<description>跳转修改资料页面</description>
<servlet-name>ModifyJumpServlet</servlet-name>
<servlet-class>com.david.Servlet.ModifyJumpServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:注册</description>
<servlet-name>GpsRegServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsRegServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:登录</description>
<servlet-name>GpsLoginServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsLoginServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:用户信息</description>
<servlet-name>GpsUserinfoServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsUserinfoServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:商品信息</description>
<servlet-name>GpsCommodityServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsCommodityServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:管理员信息</description>
<servlet-name>GpsAdmininfoServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsAdmininfoServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:修改资料</description>
<servlet-name>GpsModifyServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsModifyServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:登记出租</description>
<servlet-name>GpsRegisterServle</servlet-name>
<servlet-class>com.david.Servlet.GpsRegisterServle</servlet-class>
</servlet>
<servlet>
<description>导航栏:用户账单</description>
<servlet-name>GpsUserbillServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsUserbillServlet</servlet-class>
</servlet>
<servlet>
<description>导航栏:用户结账</description>
<servlet-name>GpsSettleaccountsServlet</servlet-name>
<servlet-class>com.david.Servlet.GpsSettleaccountsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GpsSettleaccountsServlet</servlet-name>
<url-pattern>/Gpssettleaccounts.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsUserbillServlet</servlet-name>
<url-pattern>/Gpsuserbill.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsRegisterServle</servlet-name>
<url-pattern>/Gpsregister.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsModifyServlet</servlet-name>
<url-pattern>/Gpsmodify.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsAdmininfoServlet</servlet-name>
<url-pattern>/Gpsadmininfo.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsCommodityServlet</servlet-name>
<url-pattern>/Gpscommodity.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsUserinfoServlet</servlet-name>
<url-pattern>/Gpsuserinfo.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsLoginServlet</servlet-name>
<url-pattern>/Gpslogin.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GpsRegServlet</servlet-name>
<url-pattern>/Gpsreg.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/reg.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ExitServlet</servlet-name>
<url-pattern>/logout.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CommodityServlet</servlet-name>
<url-pattern>/commodity.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RentCarServlet</servlet-name>
<url-pattern>/rentcarcommodity.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserBillServlet</servlet-name>
<url-pattern>/userbill.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SurrenderServlet</servlet-name>
<url-pattern>/billsettleaccounts.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SettleAccountsServlet</servlet-name>
<url-pattern>/settleaccounts.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ModifyServlet</servlet-name>
<url-pattern>/modify.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterJumpServlet</servlet-name>
<url-pattern>/registerjump.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegJumpServlet</servlet-name>
<url-pattern>/regjump.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ModifyJumpServlet</servlet-name>
<url-pattern>/modifys.html</url-pattern>
</servlet-mapping>
<!-- 过滤器 -->
<filter>
<!-- 过滤器名称 -->
<filter-name>CharacterEncodingFilter</filter-name>
<!-- 过滤器类 -->
<filter-class>com.david.code.CharacterEncodingFilter</filter-class>
<!-- 初始化参数 -->
<init-param>
<!-- 参数名称 -->
<param-name>encoding</param-name>
<!-- 参数值 -->
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 过滤器映射 -->
<filter-mapping>
<!-- 过滤器名称 -->
<filter-name>CharacterEncodingFilter</filter-name>
<!-- URL模式 -->
<url-pattern>/*</url-pattern>
<!-- 调度 -->
<dispatcher>REQUEST</dispatcher>
<!-- 调度 -->
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
项目开头我做了一个欢迎页面index.jsp
<%@ 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>
<title>'index.jsp'</title>
</head>
<body>
<jsp:forward page="login.jsp"></jsp:forward>
</body>
</html>
进入项目后会跳转到登录页面login.jsp
<%@ 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>用户登录</title>
<script type="text/javascript">
//登录前验证
function login(form){
if(form.username.value == " "){
alert("用户名不能为空!");
return false;
}
if(form.password.value == " "){
alert("密码不能为空!");
return false;
}
}
</script>
</head>
<body>
<div align="center">
<div>
<table align="center" border="0" width="800">
<tr>
<td align="center" colspan="8"><h2>欢迎光临答答租车系统</h2></td>
</tr>
<tr>
<td><a href="Gpsreg.html">用户注册</a></td>
<td><a href="Gpslogin.html">用户登录</a></td>
<td><a href="Gpsuserinfo.html">用户信息</a></td>
<td><a href="Gpscommodity.html">商品信息</a></td>
<td><a href="Gpsadmininfo.html">管理员信息</a></td>
<td><a href="Gpsmodify.html">修改资料</a></td>
<td><a href="Gpsregister.html">登记出租</a></td>
<td><a href="Gpsuserbill.html">用户账单</a></td>
</tr>
</table>
</div>
<br><hr><hr><br>
<%
//获取提示信息
String info = (String)request.getAttribute("info");
//如果提示信息不为空则输出提示信息
if(info != null){
out.println(info);
}
%>
<div>
<form action="login.html" method="post" onsubmit="retrun login(this)">
<table align="center" border="0" width="400">
<tr><td align="center" colspan="2"><h3>用户登录</h3></td></tr>
<tr>
<td align="left">用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td align="left">密 码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<a href="regjump.html"><input type="button" value="注册"></a>
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
登录页面可以注册新用户,点击注册按钮,通过web.xml中配置好的servlet信息,找到并执行RegJumpServlet
package com.david.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 跳转注册页面 servlet
* Servlet implementation class RegJumpServlet
*/
public class RegJumpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegJumpServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.getRequestDispatcher("reg.jsp").forward(request, response);
}
}
然后会跳转到reg.jsp页面注册
<%@ 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>用户注册</title>
<script type="text/javascript">
function reg(form){
if(form.username.value == " "){
alert("用户名不能为空!");
return false;
}
if(form.password.value == " "){
alert("密码不能为空!");
return false;
}
if(form.sex.value == " "){
alert("性别不能为空!");
return false;
}
if(form.tal.value == " "){
alert("联系电话不能为空!");
return false;
}
if(form.email.value == " "){
alert("电子邮箱不能为空!");
return false;
}
}
</script>
</head>
<body>
<div align="center">
<div>
<table align="center" border="0" width="800">
<tr>
<td align="center" colspan="8"><h2>欢迎光临答答租车系统</h2></td>
</tr>
<tr>
<td><a href="Gpsreg.html">用户注册</a></td>
<td><a href="Gpslogin.html">用户登录</a></td>
<td><a href="Gpsuserinfo.html">用户信息</a></td>
<td><a href="Gpscommodity.html">商品信息</a></td>
<td><a href="Gpsadmininfo.html">管理员信息</a></td>
<td><a href="Gpsmodify.html">修改资料</a></td>
<td><a href="Gpsregister.html">登记出租</a></td>
<td><a href="Gpsuserbill.html">用户账单</a></td>
</tr>
</table>
</div>
<br><hr><hr><br>
<div>
<form action="reg.html" method="post" onclick="return reg(this)">
<table>
<tr>
<td align="center" colspan="2"><h3>欢迎注册,请仔细填写下面的资料</h3></td>
</tr>
<tr>
<td align="center">用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td align="center">密码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td align="center">性别:</td>
<td><input type="text" name="sex"></td>
</tr>
<tr>
<td align="center">联系电话:</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td align="center">电子邮箱:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="注册">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
这里用的是表单提交功能,填写完资料后点击注册按钮,会将资料表单通过web.xml中配置好的信息找到并执行RegServlet
package com.david.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.david.Account;
import com.david.User;
import com.david.dao.AccountDao;
import com.david.dao.UserDao;
/**
* 注册 servlet
* Servlet implementation class RegServlet
*/
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取用户名
String username = request.getParameter("username");
//获取用户密码
String password = request.getParameter("password");
//获取性别
String sex = request.getParameter("sex");
//获取联系电话
String tel = request.getParameter("tel");
//获取电子邮箱
String email = request.getParameter("email");
//实例化UserDao对象
UserDao userDao = new UserDao();
//实例化AccountDao对象
AccountDao accountDao = new AccountDao();
//调用方法判断用户在数据库中是否存在
if(username != null && !username.isEmpty()){
if(userDao.userIsExsit(username)){
//实例化一个用户对象
User user = new User();
//对用户对象进行赋值
user.setUsername(username);
user.setPassword(password);
user.setSex(sex);
user.setTel(tel);
user.setEmail(email);
user.setCompetence("普通用户");
//保存用户注册信息
userDao.saveUser(user);
//定义变量保存查询到的用户id
int userid = userDao.userid(username);
//实例化一个账户对象
Account account = new Account();
//对账户对象进行赋值
account.setUserid(userid);
account.setMoney((double) 0);
account.setLet_frequency(0);
account.setOut_frequency(0);
//保存账户信息
accountDao.saveAccount(account);
//注册成功的提示信息
request.setAttribute("info", "恭喜,注册成功!您可以登录开始租车啦!");
}else{
//注册失败的提示信息
request.setAttribute("info", "错误,用户名已存在!");
}
}
//转发到login.jsp页面
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
在RegServlet中会接受页面输入的资料信息,然后调用UserDao中的方法将数据添加到数据库中并同步更新数据库,同时会初始化这个新用户的账户信息,最后提示注册成功并返回到登录页面,输入账号密码,点击登录按钮,通过web.xml中配置好的信息找到并执行LoginServlet
package com.david.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.david.Account;
import com.david.User;
import com.david.dao.AccountDao;
import com.david.dao.UserDao;
/**
* 用户登录 Servlet
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取用户名
String username = request.getParameter("username");
//获取密码
String password = request.getParameter("password");
//实例化UserDao对象
UserDao userDao = new UserDao();
//根据用户名密码查询用户
User user = userDao.login(username, password);
//判断用户是否为空
if(user != null){
//将用户对象放入session中
request.getSession().setAttribute("user", user);
//根据用户权限跳转不同的页面
if("普通用户".equals(user.getCompetence()) || "贵宾用户".equals(user.getCompetence())){
//实例化一个账户对象
AccountDao accountDao = new AccountDao();
//根据用户id查询账户
Account account = accountDao.useridaccount(user.getId());
request.setAttribute("account", account);
//转发到 userinfo.jsp 页面
request.getRequestDispatcher("userinfo.jsp").forward(request, response);
}else if("管理员用户".equals(user.getCompetence())){
//转发到admininfo.jsp页面
request.getRequestDispatcher("admininfo.jsp").forward(request, response);
}else{
}
}else{
//登录失败
request.setAttribute("info", "错误:用户名或密码错误!");
//转发到 userinfo.jsp 页面
request.getRequestDispatcher("userinfo.jsp").forward(request, response);
}
}
}
在LoginServlet中会接受用户输入的账户密码信息,并调用中的方法验证用户密码是否正确,验证完毕判断用户权限,根据不同的权限跳转不同的页面,这里因为目前超级管理员功能还没扩展出来,所以第二个else暂时是空的
热门评论
主函数我并没有找到呢~请问在哪呢?