现在的位置: 首页 > 综合 > 正文

java+mvc登录注册源码

2018年03月31日 ⁄ 综合 ⁄ 共 27619字 ⁄ 字号 评论关闭

有史以来最全的初学者教程来了。。。

本博客最适合初学者,是最基本的mvc使用,实现的功能:登录、注册、找回密码,用户查询,用户删除。主要是练习最基本的sql操作等。废话不多说,我来告诉你什么是实例O(∩_∩)O哈哈~

1、开发环境:windows xp+myEclipse 6.6+Tomcat 6.0+mysql 5.0

2、搭建struts环境,自己去翻其他实例教程,这里不赘述,项目目录如下。

3、数据库连接是用连接池的方式来连接的,web.xml,applicationContext.xml,proxool.xml这三个文件配置好,写出连接类就能连接成功了。

(1)web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 


<!--启动连接池-->
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>
WEB-INF/proxool.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>\WEB-INF\applicationContext.xml</param-value> 
</context-param> 
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

<servlet> 
<servlet-name>CXFServlet</servlet-name> 
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
<load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>CXFServlet</servlet-name> 
<url-pattern>/Service/*</url-pattern> 
</servlet-mapping> 

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

</web-app> 
(2)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation=" 

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
default-lazy-init="true"> 

<import resource="classpath:META-INF/cxf/cxf.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDataSource</value> 
</property>
<property name="url">
<value>proxool.ZHKS</value> 
</property>
</bean> 
</beans> 

(3)  proxool.xml

<?xml version="1.0" encoding="UTF-8"?>
<proxool-config>
<proxool>
<alias>ZHKS</alias>
<driver-url>jdbc:mysql://192.168.136.127:3306/user?useUnicode=true&characterEncoding=UTF-8</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="root" />
</driver-properties>
<maximum-new-connections>100</maximum-new-connections>
<prototype-count>5</prototype-count>

<house-keeping-sleep-time>60000</house-keeping-sleep-time> 
<house-keeping-test-sql>select current_date from dual</house-keeping-test-sql>
<maximum-connection-count>5000</maximum-connection-count>
<minimum-connection-count>2</minimum-connection-count>
</proxool>
</proxool-config>

(4)com.cvicse.DBconn.DBConnection.java

package com.cvicse.DBconn;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;

public class DBConnection {
public Connection getConn() {
Driver driver;
Connection conn = null;
try {
driver = (Driver)Class.forName("org.logicalcobwebs.proxool.ProxoolDriver").newInstance();
DriverManager.registerDriver(driver);
conn = DriverManager.getConnection("proxool.ZHKS");
//conn = DriverManager.getConnection("proxool.ZHKS:driver:jdbc:mysql://192.168.136.127:3306/user","root","root");
} catch (Exception e1) {
e1.printStackTrace();
}
return conn;
}
}

(5)sql脚本

CREATE TABLE `user` (
`userid` int(10) NOT NULL auto_increment COMMENT '用户编号',
`username` varchar(30) default NULL COMMENT '用户名',
`password` varchar(30) default NULL COMMENT '密码',
`email` varchar(50) default NULL COMMENT '邮箱',
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

现在我们已经把数据库给连接上了,下面就进入功能的编写

4、com.cvicse.bean.LoginForm.java

package com.cvicse.bean;

public class LoginForm {

private int userid;
private String username;
private String password;
private String email;

/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the userid
*/
public int getUserid() {
return userid;
}
/**
* @param userid the userid to set
*/
public void setUserid(int userid) {
this.userid = userid;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}

}

5、com.cvicse.bean.Mail.java

package com.cvicse.bean;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
* @author ------
*
*/
public class Mail {

String to = ""; // 收件人 
String from = ""; // 发件人 
String host = ""; // smtp主机 
String username = ""; // 用户名 
String password = ""; // 密码 
String subject = ""; // 邮件主题 
String content = ""; // 邮件正文 

public Mail() { 
} 

public Mail(String to, String from, String host, String username, 
String password, String subject, String content) { 
this.to = to; 
this.from = from; 
this.host = host; 
this.username = username; 
this.password = password; 
this.subject = subject; 
this.content = content; 
}

/** 
* 发送邮件 
* 
* @return 成功返回true,失败返回false 
*/
public boolean sendMail() {
//构造mail session
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.qiye.163.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});

try {
//构造MimeMessage并设定基本的值,创建消息对象
MimeMessage msg = new MimeMessage(session);
//设置消息内容
msg.setFrom(new InternetAddress(from));
System.out.println("1"+from);
//把邮件地址映射到Internet地址上
InternetAddress[] address = {new InternetAddress(to)};
//有两个参数,第一个参数是接收者的类型,第二个参数是接收者。
msg.setRecipients(Message.RecipientType.TO, address);
//设置邮件的标题
subject = transferChinese(subject);
msg.setSubject(subject);
//构造Multipart
Multipart mp = new MimeMultipart();
//向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
// 设置邮件内容(纯文本格式) 
/* mbpContent.setText(content);*/ 
// 设置邮件内容(HTML格式) 
mbpContent.setContent(content, "text/html;charset=utf-8");
//向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent);
//向Multipart添加MimeMessage
msg.setContent(mp);
//设置邮件发送时间
msg.setSentDate(new Date());
//发送邮件
Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
return true;
}

/** 
* 把主题转换为中文 
* 
* @param strText 
* @return 
*/ 
public String transferChinese(String strText) { 

try { 
strText = MimeUtility.encodeText(new String(strText.getBytes(), 
"GB2312"), "GB2312", "B"); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

return strText; 
} 

/**
* @return the to
*/
public String getTo() {
return to;
}
/**
* @param to the to to set
*/
public void setTo(String to) {
this.to = to;
}
/**
* @return the from
*/
public String getFrom() {
return from;
}
/**
* @param from the from to set
*/
public void setFrom(String from) {
this.from = from;
}
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the subject
*/
public String getSubject() {
return subject;
}
/**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
}

6、com.cvicse.action.LoginAction.java

package com.cvicse.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.cvicse.bean.LoginForm;
import com.cvicse.bean.Mail;
import com.cvicse.service.LoginService;
import com.cvicse.service.impl.LoginServiceImpl;
import com.opensymphony.xwork2.ActionSupport;



@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{

LoginService loginService = new LoginServiceImpl();
HttpServletRequest request = ServletActionContext.getRequest();
/**
* 用户登录
* @return
*/ 
@SuppressWarnings("unchecked")
public String login(){

HttpServletRequest request = ServletActionContext.getRequest();
String username = request.getParameter("username");
String password = request.getParameter("password");

LoginForm loginForm = new LoginForm();
loginForm.setUsername(username);
loginForm.setPassword(password);


try {
List ishave = loginService.ishave_user(username);
if (ishave != null && !ishave.isEmpty()) {
int flag = loginService.loginCheck(loginForm);
if (flag == 1) {
request.setAttribute("info", "恭喜,登录成功!");
}else {
request.setAttribute("info", "对不起,用户名和密码不匹配,请重试!");
return "failure";
}
}else {
request.setAttribute("info", "对不起,用户名不存在!");
return "failure";
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
/**
* 用户注册
* @return
*/
public String register() {
HttpServletRequest request = ServletActionContext.getRequest();
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
/*LoginService loginService = new LoginServiceImpl();*/
try {
int is_occupied = loginService.is_occupied(username);
if (is_occupied == 1) {
request.setAttribute("info", "对不起,用户名已被占用,请重新输入!");
return "failure";
}else {
boolean flag = loginService.register(username, password,email);
if (flag == true) {
request.setAttribute("info", "恭喜,注册成功!");
}else {
request.setAttribute("info", "对不起,注册失败!");
return "failure";
}
}
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}

return SUCCESS;
}
@SuppressWarnings("unchecked")
public String allUser() {
HttpServletRequest request = ServletActionContext.getRequest();
try {
List list = loginService.allUser();
request.setAttribute("info", "恭喜,查询成功!");
request.setAttribute("userlist", list);
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
public String deleteByUserId() {
HttpServletRequest request = ServletActionContext.getRequest();
int userid = Integer.parseInt(request.getParameter("userid"));
try {
boolean flag = loginService.deleteByUserId(userid);
if (flag == false) {
request.setAttribute("info", "对不起,删除失败!");
}
allUser();
} catch (Exception e) {
// Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
/**
* 找回密码
*/
@SuppressWarnings("unchecked")
public String findPassWord() {
String username = request.getParameter("username");
try {
List ishave = loginService.ishave_user(username);
if (ishave != null && !ishave.isEmpty()) {

LoginForm infoForm=(LoginForm)ishave.get(0);
String toMail = infoForm.getEmail();

//String toMail = inForm.getEmail().toString();
System.out.println("2"+toMail);

StringBuffer strbuf = new StringBuffer();
strbuf.append("亲爱的用户 tjcyjd:您好!<br><br>");
strbuf.append(" 您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码。假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员。<br><br>");
strbuf.append(" 要使用新的密码, 请使用以下链接启用密码。<br><br>");
strbuf.append(" <a href='http://passport.csdn.net/account/resetpassword?user=tjcyjd&active=jJTi9HgBmARmyittIJ7fBvzCtbvaz6FCXj0ZXJpn940=0'>http://passport.csdn.net/account/resetpassword?user=tjcyjd&active=jJTi9HgBmARmyittIJ7fBvzCtbvaz6FCXj0ZXJpn940=0</a>");
strbuf.append("<br><br>我们将一如既往、热忱的为您服务!");
strbuf.append("<br><br>WWW.CSDN.NET - 中国最大的IT技术社区,为IT专业技术人员提供最全面的信息传播和服务平台");

/** strm[1]第一个跟第二个@间内容,strm[strm.length - 1]最后一@内容 */
String strm[] = toMail.split("@");
Mail mail = new Mail();//创建邮件
mail.setTo(toMail);
mail.setFrom("su_qiang@cvicse.com");
mail.setHost("smtp.qiye.163.com"); 
mail.setUsername("su_qiang@cvicse.com");// 用户 
mail.setPassword("sq1234");// 密码 
mail.setSubject("[Test]find your password"); 
mail.setContent(strbuf.toString());
if (mail.sendMail()) { 
request.setAttribute("info", "您的申请已提交成功,请查看您的******" + strm[strm.length - 1]+ "邮箱。");
} else { 
request.setAttribute("info","操作失败,请重试!"); 
}
}else {
request.setAttribute("info", "对不起,用户名不存在!");
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}

}

7、com.cvicse.dao.LoginDao.java

/**
* 
*/
package com.cvicse.dao;

import java.util.List;

import com.cvicse.bean.LoginForm;

/**
* @author ------
*
*/
@SuppressWarnings("unchecked")
public interface LoginDao {

public int loginCheck(LoginForm loginForm)throws Exception;
public List ishave_user(String username)throws Exception;
public boolean register(String username,String password,String email)throws Exception;
public int is_occupied(String username) throws Exception;
public List allUser() throws Exception;
public boolean deleteByUserId(int userid) throws Exception;

}

8、com.cvicse.dao.impl.LoginDaoImpl.java

/**
* 
*/
package com.cvicse.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


import com.cvicse.DBconn.DBConnection;
import com.cvicse.bean.LoginForm;
import com.cvicse.dao.LoginDao;
import com.mysql.jdbc.Connection;

/**
* 
* @author ------
*
*/
public class LoginDaoImpl implements LoginDao {

DBConnection dbConnection = new DBConnection();
Connection connection = (Connection) dbConnection.getConn();

PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
// 用户登录-验证用户名密码是否匹配
public int loginCheck(LoginForm loginForm) throws Exception {
// Auto-generated method stub
String username = loginForm.getUsername();
String password = loginForm.getPassword();

int flag = 0;
// String sqlString = "select username,password from user where username='"+username+"' and password='"+password+"'";
String sqlString = "select username,password from user where username=? and password=?";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
flag = 1;
}
return flag;

}
@SuppressWarnings("unchecked")
public List ishave_user(String username) throws Exception {
// Auto-generated method stub
String sqlString = "select userid,username,password,email from user where username='"+username+"'";
preparedStatement = connection.prepareStatement(sqlString);
resultSet = preparedStatement.executeQuery();
List list = new ArrayList();
if (resultSet.next()) {
LoginForm loginForm = new LoginForm();
loginForm.setUserid(resultSet.getInt("userid"));
loginForm.setUsername(resultSet.getString("username"));
loginForm.setPassword(resultSet.getString("password"));
loginForm.setEmail(resultSet.getString("email"));
list.add(loginForm);
}
return list;
}
// 用户注册
public boolean register(String username, String password,String email) throws Exception {
// Auto-generated method stub

boolean flag = false;
String sqlString = "insert into user(username,password,email) values(?,?,?)";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
preparedStatement.setString(3, email);
// preparedStatement.executeUpdate();
if (preparedStatement.executeUpdate() > 0) {
flag = true;
}
preparedStatement.close();
return flag;
}

// 验证用户名是否重复
public int is_occupied(String username) throws Exception {
// Auto-generated method stub
int flag = 0;
String sqlString = "select username from user where username='"
+ username + "'";
preparedStatement = connection.prepareStatement(sqlString);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
flag = 1;
}
return flag;
}

@SuppressWarnings("unchecked")
public List allUser() throws Exception {
// Auto-generated method stub
List list = new ArrayList();
String sqlString = "select * from user";
preparedStatement = connection.prepareStatement(sqlString);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
LoginForm loginForm = new LoginForm();
loginForm.setUserid(resultSet.getInt("userid"));
loginForm.setUsername(resultSet.getString("username"));
loginForm.setPassword(resultSet.getString("password"));
loginForm.setEmail(resultSet.getString("email"));
list.add(loginForm);
}
resultSet.close();
preparedStatement.close();
return list;
}

public boolean deleteByUserId(int userid) throws Exception {
// Auto-generated method stub
String sqlString = "delete from user where userid =" + userid;
preparedStatement = connection.prepareStatement(sqlString);
/* preparedStatement.setInt(1, userid); */
boolean flag = false;
if (preparedStatement.executeUpdate() > 0) {
flag = true;
}
preparedStatement.close();
return flag;
}

}

9、com.cvicse.service.LoginService.java

/**
* 
*/
package com.cvicse.service;

import java.util.List;

import com.cvicse.bean.LoginForm;

/**
* @author ------
*
*/
@SuppressWarnings("unchecked")
public interface LoginService {

public int loginCheck(LoginForm loginForm)throws Exception;
public List ishave_user(String username)throws Exception;
public boolean register(String username,String password,String email)throws Exception;
public int is_occupied(String username) throws Exception;
public List allUser() throws Exception;
public boolean deleteByUserId(int userid) throws Exception;

}

10、com.cvicse.service.impl.LoginServiceImpl.java

/**
* 
*/
package com.cvicse.service.impl;

import java.util.List;

import com.cvicse.bean.LoginForm;
import com.cvicse.dao.LoginDao;
import com.cvicse.dao.impl.LoginDaoImpl;
import com.cvicse.service.LoginService;



/**
* @author su_qiang
*
*/
public class LoginServiceImpl implements LoginService{

LoginDao loginDao = new LoginDaoImpl();
public int loginCheck(LoginForm loginForm) throws Exception {
// Auto-generated method stub
/*LoginDao loginDao = new LoginDaoImpl();*/
int flag = loginDao.loginCheck(loginForm);
return flag;
}
/*
* (non-Javadoc)
* @see com.cvicse.service.LoginService#ishave_user(java.lang.String)
*/
@SuppressWarnings("unchecked")
public List ishave_user(String username) throws Exception {
// Auto-generated method stub
List ishave = loginDao.ishave_user(username);
return ishave;
}

public boolean register(String username, String password,String email) throws Exception {
// Auto-generated method stub
/*LoginDao loginDao = new LoginDaoImpl();*/
boolean flag = loginDao.register(username, password,email);
return flag;
}

public int is_occupied(String username) throws Exception {
// Auto-generated method stub
int flag = loginDao.is_occupied(username);
return flag;
}

@SuppressWarnings("unchecked")
public List allUser() throws Exception {
// Auto-generated method stub
List list = loginDao.allUser();
return list;
}

public boolean deleteByUserId(int userid) throws Exception {
// Auto-generated method stub
boolean flag = loginDao.deleteByUserId(userid);
return flag;
}

}

11、struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />

<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.cvicse.action.LoginAction" method="login" >
<result name="success">loginSuccess.jsp</result>
<result name="failure">login.jsp</result>
</action>
<action name="register" class="com.cvicse.action.LoginAction" method="register" >
<result name="success">login.jsp</result>
<result name="failure">register.jsp</result>
</action>
<action name="allUser" class="com.cvicse.action.LoginAction" method="allUser" >
<result name="success">loginSuccess.jsp</result>
</action>
<action name="delete" class="com.cvicse.action.LoginAction" method="deleteByUserId" >
<result name="success">loginSuccess.jsp</result>
</action>
<action name="forgetPwd" class="com.cvicse.action.LoginAction" method="findPassWord" >
<result name="success">forgetpwd.jsp</result>
</action> 
</package>
</struts>

12、单元测试:com.cvicse.test.LoginActionTest.java,

    com.cvicse.dao.TestDao.java,
    com.cvicse.dao.impl.TestDaoImpl.java,
    com.cvicse.service.TestService.java,
    com.cvicse.service.impl.TestServiceImpl.java

现在我们配置文件struts有了,Action有了,service有了,dao有了,都有了,O(∩_∩)O哈哈~,单元测试自己做下体验一下

前台页面如下:

13、login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>suqiang exercise</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<!-- -->
</head>
<script type="text/javascript">
function check(){
var name = document.getElementById("username").value;
var pwd = document.getElementById("password").value;
if(name == "" || name == null){
document.getElementById("tip").innerHTML="用户名不能为空哦!";
return false;
}
if(pwd == "" || pwd == null){
document.getElementById("tip").innerHTML="密码不能为空哦!";
return false;
}
logForm.submit();
}
function codefans(){
var box=document.getElementById("tip");
box.style.display="none"; 
}
setTimeout("codefans()",3000);
</script>
<body onload="javascript:document.getElementById('username').focus();">
<form name="logForm" action="login.action" method="post">
<table width="30%" id="mytab" border="1" class="t1">
<tr class="a1">
<th colspan="3" align="center">用户登录</th>
</tr>
<tr class="a1">
<th width="20%">用户名</th>
<td colspan="2"><input type="text" name="username" id="username"/></td>
</tr>
<tr class="a1">
<th width="20%">密  码</th>
<td colspan="2"><input type="password" name="password" id="password"/></td>
</tr>
<tr class="a1">
<td align="left"><a href="forgetpwd.jsp">忘记了密码?</a></td>
<td align="center"><input class="btn1_mouseout" onmouseover="this.className='btn1_mouseover'"
onmouseout="this.className='btn1_mouseout'" type="button" value="登录" onclick="check()"/></td>
<td align="right"><a href="register.jsp">还没有账号?</a></td>
</tr>
</table>
</form>
<center><div style="color:#ff0000" id="tip"><h1>${info}</h1></div></center>
</body>
</html>

14、loginSuccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'loginSuccess.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/styles.css">


</head>
<script type= "text/javascript">
function load(){
document.getElementById("tip").style.display ="";
window.location.href='allUser.action';
}
</script>
<body>
<center><div id="tip"><h1 style="color:#ff0000"><%=request.getAttribute("info") %></h1></div></center>
<table width="90%" id="mytab" border="1" class="t1">
<thead>
<th width="15%">用户编号</th> 
<th width="30%">用户名</th> 
<th width="30%">用户密码</th> 
<th width="15%">操作</th> 
</thead> 
<c:forEach items="${userlist}" var="list">
<tr class="a1"> 
<td align = "center">${list.userid}</td> 
<td align = "center">${list.username}</td> 
<td align = "center">${list.password}</td> 
<td align = "center"><a href="">修改</a>|<a href="delete.action?userid=${list.userid}">删除</a></td>
</tr>
</c:forEach> 
</table>
<center><input type="submit" value="查看用户列表" onclick="load()"/></center>
</body>
</html>

15、register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>suqiang exercise</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/styles.css">


</head>
<script type="text/javascript">
function check(){
var name = document.getElementById("username").value;
var pwd = document.getElementById("password").value;
var email = document.getElementById("email").value;
if(name == "" || name == null){
document.getElementById("tip").innerHTML="用户名不能为空哦!";
return false;
}
if(pwd == "" || pwd == null){
document.getElementById("tip").innerHTML="密码不能为空哦!";
return false;
}
if(email == "" || email == null){
document.getElementById("tip").innerHTML="邮箱必须填写,方便您找回密码。";
return false;
}
regForm.submit();
}
function codefans(){
var box=document.getElementById("tip");
box.style.display="none"; 
}
setTimeout("codefans()",6000);//2秒改
</script>
<body>
<form name="regForm" action="register.action" method="post">
<table width="30%" id="mytab" border="1" class="t1">
<tr class="a1">
<th colspan="3" align="center">用户注册</th>
</tr>
<tr class="a1">
<th width="20%">用户名</th>
<td colspan="2"><input type="text" name="username" id="username"/></td>
</tr>
<tr class="a1">
<th width="20%">密  码</th>
<td colspan="2"><input type="password" name="password" id="password"/></td>
</tr>
<tr class="a1">
<th width="20%">邮  箱</th>
<td colspan="2"><input type="text" name="email" id="email"/></td>
</tr>
<tr class="a1">
<td align="left"> </td>
<td align="center"><input type="button" value="注册" onclick="check()"/></td>
<td align="right"><a href="login.jsp">直接返回登录</a></td>
</tr>
</table>
</form>
<center><div style="color:#ff0000" id="tip"><h1>${info}</h1></div></center>
</body>
</html>

16、forgetpwd.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> 
<title>My JSP 'forgetpwd.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<script type="text/javascript">
function check(){
var name = document.getElementById("username").value;
if(name == "" || name == null){
document.getElementById("tip").innerHTML="用户名不能为空哦!";
return false;
}
pwdForm.submit();
}
function codefans(){
var box=document.getElementById("tip");
box.style.display="none"; 
}
setTimeout("codefans()",3000);
</script>
<body>
<form name="pwdForm" action="forgetPwd.action" method="post">
<table width="30%" id="mytab" border="1" class="t1">
<tr class="a1">
<th colspan="3" align="center">找回密码</th>
</tr>
<tr class="a1">
<th width="20%">用户名</th>
<td colspan="2"><input type="text" name="username" id="username"/></td>
</tr>
<tr class="a1">
<td align="left"></td>
<td align="center"><input type="button" value="下一步" onclick="check()"/></td>
<td align="right"></td>
</tr>
</table>
</form>
<center><div style="color:#ff0000" id="tip"><h1>${info}</h1></div></center>
</body>
</html>

17,、styles.css

@CHARSET "UTF-8";
body,table{
font-size:12px;
}
table{
table-layout:fixed;
empty-cells:show; 
border-collapse: collapse;
margin:0 auto;
vertical-align:middle;
}
td{
height:20px;
}
h1,h2,h3{
font-size:12px;
margin:0;
padding:0;
}

.title { background: #FFF; border: 1px solid #9DB3C5; padding: 1px; width:90%;margin:20px auto; }
.title h1 { line-height: 31px; text-align:center; background: #2F589C url(th_bg2.gif); background-repeat: repeat-x; background-position: 0 0; color: #FFF; }
.title th, .title td { border: 1px solid #CAD9EA; padding: 5px; }


/*这个是借鉴一个论坛的样式*/
table.t1{
border:1px solid #cad9ea;
color:#666;
}
table.t1 th {
background-image: url(th_bg1.gif);
background-repeat::repeat-x;
height:30px;
}
table.t1 td,table.t1 th{
border:1px solid #cad9ea;
padding:0 1em 0;
}
table.t1 tr.a1{
background-color:#f5fafe;
}

抱歉!评论已关闭.