本博客最适合初学者,是最基本的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

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <!--启动连接池-->
  7. <servlet>
  8. <servlet-name>ServletConfigurator</servlet-name>
  9. <servlet-class>
  10. org.logicalcobwebs.proxool.configuration.ServletConfigurator
  11. </servlet-class>
  12. <init-param>
  13. <param-name>xmlFile</param-name>
  14. <param-value>
  15. WEB-INF/proxool.xml
  16. </param-value>
  17. </init-param>
  18. <load-on-startup>1</load-on-startup>
  19. </servlet>
  20. <context-param>
  21. <param-name>contextConfigLocation</param-name>
  22. <param-value>\WEB-INF\applicationContext.xml</param-value>
  23. </context-param>
  24. <listener>
  25. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  26. </listener>
  27. <servlet>
  28. <servlet-name>CXFServlet</servlet-name>
  29. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  30. <load-on-startup>2</load-on-startup>
  31. </servlet>
  32. <servlet-mapping>
  33. <servlet-name>CXFServlet</servlet-name>
  34. <url-pattern>/Service/*</url-pattern>
  35. </servlet-mapping>
  36. <filter>
  37. <filter-name>struts2</filter-name>
  38. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  39. </filter>
  40. <filter-mapping>
  41. <filter-name>struts2</filter-name>
  42. <url-pattern>*.action</url-pattern>
  43. </filter-mapping>
  44. </web-app>
  45. (2)applicationContext.xml
  46. <?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49. xmlns:jaxws="http://cxf.apache.org/jaxws"
  50. xsi:schemaLocation="
  51. http://www.springframework.org/schema/beans
  52. http://www.springframework.org/schema/beans/spring-beans.xsd
  53. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
  54. default-lazy-init="true">
  55. <import resource="classpath:META-INF/cxf/cxf.xml" />
  56. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  57. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  58. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  59. <property name="driverClassName">
  60. <value>org.logicalcobwebs.proxool.ProxoolDataSource</value>
  61. </property>
  62. <property name="url">
  63. <value>proxool.ZHKS</value>
  64. </property>
  65. </bean>
  66. </beans>

(3)  proxool.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <proxool-config>
  3. <proxool>
  4. <alias>ZHKS</alias>
  5. <driver-url>jdbc:mysql://192.168.136.127:3306/user?useUnicode=true&characterEncoding=UTF-8</driver-url>
  6. <driver-class>com.mysql.jdbc.Driver</driver-class>
  7. <driver-properties>
  8. <property name="user" value="root" />
  9. <property name="password" value="root" />
  10. </driver-properties>
  11. <maximum-new-connections>100</maximum-new-connections>
  12. <prototype-count>5</prototype-count>
  13. <house-keeping-sleep-time>60000</house-keeping-sleep-time>
  14. <house-keeping-test-sql>select current_date from dual</house-keeping-test-sql>
  15. <maximum-connection-count>5000</maximum-connection-count>
  16. <minimum-connection-count>2</minimum-connection-count>
  17. </proxool>
  18. </proxool-config>

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

[java]  view plain copy
  1. package com.cvicse.DBconn;
  2. import java.sql.Connection;
  3. import java.sql.Driver;
  4. import java.sql.DriverManager;
  5. public class DBConnection {
  6. public Connection getConn() {
  7. Driver driver;
  8. Connection conn = null;
  9. try {
  10. driver = (Driver)Class.forName("org.logicalcobwebs.proxool.ProxoolDriver").newInstance();
  11. DriverManager.registerDriver(driver);
  12. conn = DriverManager.getConnection("proxool.ZHKS");
  13. //conn = DriverManager.getConnection("proxool.ZHKS:driver:jdbc:mysql://192.168.136.127:3306/user","root","root");
  14. } catch (Exception e1) {
  15. e1.printStackTrace();
  16. }
  17. return conn;
  18. }
  19. }

(5)sql脚本

[sql]  view plain copy
  1. CREATE TABLE `user` (
  2. `userid` int(10) NOT NULL auto_increment COMMENT '用户编号',
  3. `username` varchar(30) default NULL COMMENT '用户名',
  4. `password` varchar(30) default NULL COMMENT '密码',
  5. `email` varchar(50) default NULL COMMENT '邮箱',
  6. PRIMARY KEY (`userid`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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

4、com.cvicse.bean.LoginForm.java

[java]  view plain copy
  1. package com.cvicse.bean;
  2. public class LoginForm {
  3. private int userid;
  4. private String username;
  5. private String password;
  6. private String email;
  7. /**
  8. * @return the username
  9. */
  10. public String getUsername() {
  11. return username;
  12. }
  13. /**
  14. * @param username the username to set
  15. */
  16. public void setUsername(String username) {
  17. this.username = username;
  18. }
  19. /**
  20. * @return the password
  21. */
  22. public String getPassword() {
  23. return password;
  24. }
  25. /**
  26. * @param password the password to set
  27. */
  28. public void setPassword(String password) {
  29. this.password = password;
  30. }
  31. /**
  32. * @return the userid
  33. */
  34. public int getUserid() {
  35. return userid;
  36. }
  37. /**
  38. * @param userid the userid to set
  39. */
  40. public void setUserid(int userid) {
  41. this.userid = userid;
  42. }
  43. /**
  44. * @return the email
  45. */
  46. public String getEmail() {
  47. return email;
  48. }
  49. /**
  50. * @param email the email to set
  51. */
  52. public void setEmail(String email) {
  53. this.email = email;
  54. }
  55. }

5、com.cvicse.bean.Mail.java

[java]  view plain copy
  1. package com.cvicse.bean;
  2. import java.util.Date;
  3. import java.util.Properties;
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.Multipart;
  8. import javax.mail.PasswordAuthentication;
  9. import javax.mail.Session;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.AddressException;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeBodyPart;
  14. import javax.mail.internet.MimeMessage;
  15. import javax.mail.internet.MimeMultipart;
  16. import javax.mail.internet.MimeUtility;
  17. /**
  18. * @author ------
  19. *
  20. */
  21. public class Mail {
  22. String to = ""; // 收件人
  23. String from = ""; // 发件人
  24. String host = ""; // smtp主机
  25. String username = ""; // 用户名
  26. String password = ""; // 密码
  27. String subject = ""; // 邮件主题
  28. String content = ""; // 邮件正文
  29. public Mail() {
  30. }
  31. public Mail(String to, String from, String host, String username,
  32. String password, String subject, String content) {
  33. this.to = to;
  34. this.from = from;
  35. this.host = host;
  36. this.username = username;
  37. this.password = password;
  38. this.subject = subject;
  39. this.content = content;
  40. }
  41. /**
  42. * 发送邮件
  43. *
  44. * @return 成功返回true,失败返回false
  45. */
  46. public boolean sendMail() {
  47. //构造mail session
  48. Properties props = System.getProperties();
  49. props.put("mail.smtp.host", "smtp.qiye.163.com");
  50. props.put("mail.smtp.auth", "true");
  51. Session session = Session.getDefaultInstance(props,new Authenticator(){
  52. public PasswordAuthentication getPasswordAuthentication() {
  53. return new PasswordAuthentication(username,password);
  54. }
  55. });
  56. try {
  57. //构造MimeMessage并设定基本的值,创建消息对象
  58. MimeMessage msg = new MimeMessage(session);
  59. //设置消息内容
  60. msg.setFrom(new InternetAddress(from));
  61. System.out.println("1"+from);
  62. //把邮件地址映射到Internet地址上
  63. InternetAddress[] address = {new InternetAddress(to)};
  64. //有两个参数,第一个参数是接收者的类型,第二个参数是接收者。
  65. msg.setRecipients(Message.RecipientType.TO, address);
  66. //设置邮件的标题
  67. subject = transferChinese(subject);
  68. msg.setSubject(subject);
  69. //构造Multipart
  70. Multipart mp = new MimeMultipart();
  71. //向Multipart添加正文
  72. MimeBodyPart mbpContent = new MimeBodyPart();
  73. // 设置邮件内容(纯文本格式)
  74. /* mbpContent.setText(content);*/
  75. // 设置邮件内容(HTML格式)
  76. mbpContent.setContent(content, "text/html;charset=utf-8");
  77. //向MimeMessage添加(Multipart代表正文)
  78. mp.addBodyPart(mbpContent);
  79. //向Multipart添加MimeMessage
  80. msg.setContent(mp);
  81. //设置邮件发送时间
  82. msg.setSentDate(new Date());
  83. //发送邮件
  84. Transport.send(msg);
  85. } catch (AddressException e) {
  86. e.printStackTrace();
  87. } catch (MessagingException e) {
  88. e.printStackTrace();
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * 把主题转换为中文
  95. *
  96. * @param strText
  97. * @return
  98. */
  99. public String transferChinese(String strText) {
  100. try {
  101. strText = MimeUtility.encodeText(new String(strText.getBytes(),
  102. "GB2312"), "GB2312", "B");
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. return strText;
  107. }
  108. /**
  109. * @return the to
  110. */
  111. public String getTo() {
  112. return to;
  113. }
  114. /**
  115. * @param to the to to set
  116. */
  117. public void setTo(String to) {
  118. this.to = to;
  119. }
  120. /**
  121. * @return the from
  122. */
  123. public String getFrom() {
  124. return from;
  125. }
  126. /**
  127. * @param from the from to set
  128. */
  129. public void setFrom(String from) {
  130. this.from = from;
  131. }
  132. /**
  133. * @return the host
  134. */
  135. public String getHost() {
  136. return host;
  137. }
  138. /**
  139. * @param host the host to set
  140. */
  141. public void setHost(String host) {
  142. this.host = host;
  143. }
  144. /**
  145. * @return the username
  146. */
  147. public String getUsername() {
  148. return username;
  149. }
  150. /**
  151. * @param username the username to set
  152. */
  153. public void setUsername(String username) {
  154. this.username = username;
  155. }
  156. /**
  157. * @return the password
  158. */
  159. public String getPassword() {
  160. return password;
  161. }
  162. /**
  163. * @param password the password to set
  164. */
  165. public void setPassword(String password) {
  166. this.password = password;
  167. }
  168. /**
  169. * @return the subject
  170. */
  171. public String getSubject() {
  172. return subject;
  173. }
  174. /**
  175. * @param subject the subject to set
  176. */
  177. public void setSubject(String subject) {
  178. this.subject = subject;
  179. }
  180. /**
  181. * @return the content
  182. */
  183. public String getContent() {
  184. return content;
  185. }
  186. /**
  187. * @param content the content to set
  188. */
  189. public void setContent(String content) {
  190. this.content = content;
  191. }
  192. }

6、com.cvicse.action.LoginAction.java

[java]  view plain copy
  1. package com.cvicse.action;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.apache.struts2.ServletActionContext;
  5. import com.cvicse.bean.LoginForm;
  6. import com.cvicse.bean.Mail;
  7. import com.cvicse.service.LoginService;
  8. import com.cvicse.service.impl.LoginServiceImpl;
  9. import com.opensymphony.xwork2.ActionSupport;
  10. @SuppressWarnings("serial")
  11. public class LoginAction extends ActionSupport{
  12. LoginService loginService = new LoginServiceImpl();
  13. HttpServletRequest request = ServletActionContext.getRequest();
  14. /**
  15. * 用户登录
  16. * @return
  17. */
  18. @SuppressWarnings("unchecked")
  19. public String login(){
  20. HttpServletRequest request = ServletActionContext.getRequest();
  21. String username = request.getParameter("username");
  22. String password = request.getParameter("password");
  23. LoginForm loginForm = new LoginForm();
  24. loginForm.setUsername(username);
  25. loginForm.setPassword(password);
  26. try {
  27. List ishave = loginService.ishave_user(username);
  28. if (ishave != null && !ishave.isEmpty()) {
  29. int flag = loginService.loginCheck(loginForm);
  30. if (flag == 1) {
  31. request.setAttribute("info", "恭喜,登录成功!");
  32. }else {
  33. request.setAttribute("info", "对不起,用户名和密码不匹配,请重试!");
  34. return "failure";
  35. }
  36. }else {
  37. request.setAttribute("info", "对不起,用户名不存在!");
  38. return "failure";
  39. }
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. return SUCCESS;
  44. }
  45. /**
  46. * 用户注册
  47. * @return
  48. */
  49. public String register() {
  50. HttpServletRequest request = ServletActionContext.getRequest();
  51. String username = request.getParameter("username");
  52. String password = request.getParameter("password");
  53. String email = request.getParameter("email");
  54. /*LoginService loginService = new LoginServiceImpl();*/
  55. try {
  56. int is_occupied = loginService.is_occupied(username);
  57. if (is_occupied == 1) {
  58. request.setAttribute("info", "对不起,用户名已被占用,请重新输入!");
  59. return "failure";
  60. }else {
  61. boolean flag = loginService.register(username, password,email);
  62. if (flag == true) {
  63. request.setAttribute("info", "恭喜,注册成功!");
  64. }else {
  65. request.setAttribute("info", "对不起,注册失败!");
  66. return "failure";
  67. }
  68. }
  69. } catch (Exception e) {
  70. // Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73. return SUCCESS;
  74. }
  75. @SuppressWarnings("unchecked")
  76. public String allUser() {
  77. HttpServletRequest request = ServletActionContext.getRequest();
  78. try {
  79. List list = loginService.allUser();
  80. request.setAttribute("info", "恭喜,查询成功!");
  81. request.setAttribute("userlist", list);
  82. } catch (Exception e) {
  83. // Auto-generated catch block
  84. e.printStackTrace();
  85. }
  86. return SUCCESS;
  87. }
  88. public String deleteByUserId() {
  89. HttpServletRequest request = ServletActionContext.getRequest();
  90. int userid = Integer.parseInt(request.getParameter("userid"));
  91. try {
  92. boolean flag = loginService.deleteByUserId(userid);
  93. if (flag == false) {
  94. request.setAttribute("info", "对不起,删除失败!");
  95. }
  96. allUser();
  97. } catch (Exception e) {
  98. // Auto-generated catch block
  99. e.printStackTrace();
  100. }
  101. return SUCCESS;
  102. }
  103. /**
  104. * 找回密码
  105. */
  106. @SuppressWarnings("unchecked")
  107. public String findPassWord() {
  108. String username = request.getParameter("username");
  109. try {
  110. List ishave = loginService.ishave_user(username);
  111. if (ishave != null && !ishave.isEmpty()) {
  112. LoginForm infoForm=(LoginForm)ishave.get(0);
  113. String toMail = infoForm.getEmail();
  114. //String toMail = inForm.getEmail().toString();
  115. System.out.println("2"+toMail);
  116. StringBuffer strbuf = new StringBuffer();
  117. strbuf.append("亲爱的用户 tjcyjd:您好!<br><br>");
  118. strbuf.append(" 您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码。假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员。<br><br>");
  119. strbuf.append(" 要使用新的密码, 请使用以下链接启用密码。<br><br>");
  120. 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>");
  121. strbuf.append("<br><br>我们将一如既往、热忱的为您服务!");
  122. strbuf.append("<br><br>WWW.CSDN.NET - 中国最大的IT技术社区,为IT专业技术人员提供最全面的信息传播和服务平台");
  123. /** strm[1]第一个跟第二个@间内容,strm[strm.length - 1]最后一@内容 */
  124. String strm[] = toMail.split("@");
  125. Mail mail = new Mail();//创建邮件
  126. mail.setTo(toMail);
  127. mail.setFrom("su_qiang@cvicse.com");
  128. mail.setHost("smtp.qiye.163.com");
  129. mail.setUsername("su_qiang@cvicse.com");// 用户
  130. mail.setPassword("sq1234");// 密码
  131. mail.setSubject("[Test]find your password");
  132. mail.setContent(strbuf.toString());
  133. if (mail.sendMail()) {
  134. request.setAttribute("info", "您的申请已提交成功,请查看您的******" + strm[strm.length - 1]+ "邮箱。");
  135. } else {
  136. request.setAttribute("info","操作失败,请重试!");
  137. }
  138. }else {
  139. request.setAttribute("info", "对不起,用户名不存在!");
  140. }
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144. return SUCCESS;
  145. }
  146. }

7、com.cvicse.dao.LoginDao.java

[java]  view plain copy
  1. /**
  2. *
  3. */
  4. package com.cvicse.dao;
  5. import java.util.List;
  6. import com.cvicse.bean.LoginForm;
  7. /**
  8. * @author ------
  9. *
  10. */
  11. @SuppressWarnings("unchecked")
  12. public interface LoginDao {
  13. public int loginCheck(LoginForm loginForm)throws Exception;
  14. public List ishave_user(String username)throws Exception;
  15. public boolean register(String username,String password,String email)throws Exception;
  16. public int is_occupied(String username) throws Exception;
  17. public List allUser() throws Exception;
  18. public boolean deleteByUserId(int userid) throws Exception;
  19. }

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

[java]  view plain copy
  1. /**
  2. *
  3. */
  4. package com.cvicse.dao.impl;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import com.cvicse.DBconn.DBConnection;
  10. import com.cvicse.bean.LoginForm;
  11. import com.cvicse.dao.LoginDao;
  12. import com.mysql.jdbc.Connection;
  13. /**
  14. *
  15. * @author ------
  16. *
  17. */
  18. public class LoginDaoImpl implements LoginDao {
  19. DBConnection dbConnection = new DBConnection();
  20. Connection connection = (Connection) dbConnection.getConn();
  21. PreparedStatement preparedStatement = null;
  22. ResultSet resultSet = null;
  23. // 用户登录-验证用户名密码是否匹配
  24. public int loginCheck(LoginForm loginForm) throws Exception {
  25. // Auto-generated method stub
  26. String username = loginForm.getUsername();
  27. String password = loginForm.getPassword();
  28. int flag = 0;
  29. // String sqlString = "select username,password from user where username='"+username+"' and password='"+password+"'";
  30. String sqlString = "select username,password from user where username=? and password=?";
  31. preparedStatement = connection.prepareStatement(sqlString);
  32. preparedStatement.setString(1, username);
  33. preparedStatement.setString(2, password);
  34. resultSet = preparedStatement.executeQuery();
  35. if (resultSet.next()) {
  36. flag = 1;
  37. }
  38. return flag;
  39. }
  40. @SuppressWarnings("unchecked")
  41. public List ishave_user(String username) throws Exception {
  42. // Auto-generated method stub
  43. String sqlString = "select userid,username,password,email from user where username='"+username+"'";
  44. preparedStatement = connection.prepareStatement(sqlString);
  45. resultSet = preparedStatement.executeQuery();
  46. List list = new ArrayList();
  47. if (resultSet.next()) {
  48. LoginForm loginForm = new LoginForm();
  49. loginForm.setUserid(resultSet.getInt("userid"));
  50. loginForm.setUsername(resultSet.getString("username"));
  51. loginForm.setPassword(resultSet.getString("password"));
  52. loginForm.setEmail(resultSet.getString("email"));
  53. list.add(loginForm);
  54. }
  55. return list;
  56. }
  57. // 用户注册
  58. public boolean register(String username, String password,String email) throws Exception {
  59. // Auto-generated method stub
  60. boolean flag = false;
  61. String sqlString = "insert into user(username,password,email) values(?,?,?)";
  62. preparedStatement = connection.prepareStatement(sqlString);
  63. preparedStatement.setString(1, username);
  64. preparedStatement.setString(2, password);
  65. preparedStatement.setString(3, email);
  66. // preparedStatement.executeUpdate();
  67. if (preparedStatement.executeUpdate() > 0) {
  68. flag = true;
  69. }
  70. preparedStatement.close();
  71. return flag;
  72. }
  73. // 验证用户名是否重复
  74. public int is_occupied(String username) throws Exception {
  75. // Auto-generated method stub
  76. int flag = 0;
  77. String sqlString = "select username from user where username='"
  78. + username + "'";
  79. preparedStatement = connection.prepareStatement(sqlString);
  80. resultSet = preparedStatement.executeQuery();
  81. if (resultSet.next()) {
  82. flag = 1;
  83. }
  84. return flag;
  85. }
  86. @SuppressWarnings("unchecked")
  87. public List allUser() throws Exception {
  88. // Auto-generated method stub
  89. List list = new ArrayList();
  90. String sqlString = "select * from user";
  91. preparedStatement = connection.prepareStatement(sqlString);
  92. resultSet = preparedStatement.executeQuery();
  93. while (resultSet.next()) {
  94. LoginForm loginForm = new LoginForm();
  95. loginForm.setUserid(resultSet.getInt("userid"));
  96. loginForm.setUsername(resultSet.getString("username"));
  97. loginForm.setPassword(resultSet.getString("password"));
  98. loginForm.setEmail(resultSet.getString("email"));
  99. list.add(loginForm);
  100. }
  101. resultSet.close();
  102. preparedStatement.close();
  103. return list;
  104. }
  105. public boolean deleteByUserId(int userid) throws Exception {
  106. // Auto-generated method stub
  107. String sqlString = "delete from user where userid =" + userid;
  108. preparedStatement = connection.prepareStatement(sqlString);
  109. /* preparedStatement.setInt(1, userid); */
  110. boolean flag = false;
  111. if (preparedStatement.executeUpdate() > 0) {
  112. flag = true;
  113. }
  114. preparedStatement.close();
  115. return flag;
  116. }
  117. }

9、com.cvicse.service.LoginService.java

[java]  view plain copy
  1. /**
  2. *
  3. */
  4. package com.cvicse.service;
  5. import java.util.List;
  6. import com.cvicse.bean.LoginForm;
  7. /**
  8. * @author ------
  9. *
  10. */
  11. @SuppressWarnings("unchecked")
  12. public interface LoginService {
  13. public int loginCheck(LoginForm loginForm)throws Exception;
  14. public List ishave_user(String username)throws Exception;
  15. public boolean register(String username,String password,String email)throws Exception;
  16. public int is_occupied(String username) throws Exception;
  17. public List allUser() throws Exception;
  18. public boolean deleteByUserId(int userid) throws Exception;
  19. }

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

[java]  view plain copy
  1. /**
  2. *
  3. */
  4. package com.cvicse.service.impl;
  5. import java.util.List;
  6. import com.cvicse.bean.LoginForm;
  7. import com.cvicse.dao.LoginDao;
  8. import com.cvicse.dao.impl.LoginDaoImpl;
  9. import com.cvicse.service.LoginService;
  10. /**
  11. * @author su_qiang
  12. *
  13. */
  14. public class LoginServiceImpl implements LoginService{
  15. LoginDao loginDao = new LoginDaoImpl();
  16. public int loginCheck(LoginForm loginForm) throws Exception {
  17. // Auto-generated method stub
  18. /*LoginDao loginDao = new LoginDaoImpl();*/
  19. int flag = loginDao.loginCheck(loginForm);
  20. return flag;
  21. }
  22. /*
  23. * (non-Javadoc)
  24. * @see com.cvicse.service.LoginService#ishave_user(java.lang.String)
  25. */
  26. @SuppressWarnings("unchecked")
  27. public List ishave_user(String username) throws Exception {
  28. // Auto-generated method stub
  29. List ishave = loginDao.ishave_user(username);
  30. return ishave;
  31. }
  32. public boolean register(String username, String password,String email) throws Exception {
  33. // Auto-generated method stub
  34. /*LoginDao loginDao = new LoginDaoImpl();*/
  35. boolean flag = loginDao.register(username, password,email);
  36. return flag;
  37. }
  38. public int is_occupied(String username) throws Exception {
  39. // Auto-generated method stub
  40. int flag = loginDao.is_occupied(username);
  41. return flag;
  42. }
  43. @SuppressWarnings("unchecked")
  44. public List allUser() throws Exception {
  45. // Auto-generated method stub
  46. List list = loginDao.allUser();
  47. return list;
  48. }
  49. public boolean deleteByUserId(int userid) throws Exception {
  50. // Auto-generated method stub
  51. boolean flag = loginDao.deleteByUserId(userid);
  52. return flag;
  53. }
  54. }

11、struts.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  7. <constant name="struts.devMode" value="false" />
  8. <package name="default" namespace="/" extends="struts-default">
  9. <action name="login" class="com.cvicse.action.LoginAction" method="login" >
  10. <result name="success">loginSuccess.jsp</result>
  11. <result name="failure">login.jsp</result>
  12. </action>
  13. <action name="register" class="com.cvicse.action.LoginAction" method="register" >
  14. <result name="success">login.jsp</result>
  15. <result name="failure">register.jsp</result>
  16. </action>
  17. <action name="allUser" class="com.cvicse.action.LoginAction" method="allUser" >
  18. <result name="success">loginSuccess.jsp</result>
  19. </action>
  20. <action name="delete" class="com.cvicse.action.LoginAction" method="deleteByUserId" >
  21. <result name="success">loginSuccess.jsp</result>
  22. </action>
  23. <action name="forgetPwd" class="com.cvicse.action.LoginAction" method="findPassWord" >
  24. <result name="success">forgetpwd.jsp</result>
  25. </action>
  26. </package>
  27. </struts>

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

[plain]  view plain copy
  1. com.cvicse.dao.TestDao.java,
  2. com.cvicse.dao.impl.TestDaoImpl.java,
  3. com.cvicse.service.TestService.java,
  4. com.cvicse.service.impl.TestServiceImpl.java

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

前台页面如下:

13、login.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>suqiang exercise</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <link rel="stylesheet" type="text/css" href="css/styles.css">
  17. <!-- -->
  18. </head>
  19. <script type="text/javascript">
  20. function check(){
  21. var name = document.getElementById("username").value;
  22. var pwd = document.getElementById("password").value;
  23. if(name == "" || name == null){
  24. document.getElementById("tip").innerHTML="用户名不能为空哦!";
  25. return false;
  26. }
  27. if(pwd == "" || pwd == null){
  28. document.getElementById("tip").innerHTML="密码不能为空哦!";
  29. return false;
  30. }
  31. logForm.submit();
  32. }
  33. function codefans(){
  34. var box=document.getElementById("tip");
  35. box.style.display="none";
  36. }
  37. setTimeout("codefans()",3000);
  38. </script>
  39. <body onload="javascript:document.getElementById('username').focus();">
  40. <form name="logForm" action="login.action" method="post">
  41. <table width="30%" id="mytab" border="1" class="t1">
  42. <tr class="a1">
  43. <th colspan="3" align="center">用户登录</th>
  44. </tr>
  45. <tr class="a1">
  46. <th width="20%">用户名</th>
  47. <td colspan="2"><input type="text" name="username" id="username"/></td>
  48. </tr>
  49. <tr class="a1">
  50. <th width="20%">密  码</th>
  51. <td colspan="2"><input type="password" name="password" id="password"/></td>
  52. </tr>
  53. <tr class="a1">
  54. <td align="left"><a href="forgetpwd.jsp">忘记了密码?</a></td>
  55. <td align="center"><input class="btn1_mouseout" onmouseover="this.className='btn1_mouseover'"
  56. onmouseout="this.className='btn1_mouseout'" type="button" value="登录" onclick="check()"/></td>
  57. <td align="right"><a href="register.jsp">还没有账号?</a></td>
  58. </tr>
  59. </table>
  60. </form>
  61. <center><div style="color:#ff0000" id="tip"><h1>${info}</h1></div></center>
  62. </body>
  63. </html>

14、loginSuccess.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'loginSuccess.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <link rel="stylesheet" type="text/css" href="css/styles.css">
  18. </head>
  19. <script type= "text/javascript">
  20. function load(){
  21. document.getElementById("tip").style.display ="";
  22. window.location.href='allUser.action';
  23. }
  24. </script>
  25. <body>
  26. <center><div id="tip"><h1 style="color:#ff0000"><%=request.getAttribute("info") %></h1></div></center>
  27. <table width="90%" id="mytab" border="1" class="t1">
  28. <thead>
  29. <th width="15%">用户编号</th>
  30. <th width="30%">用户名</th>
  31. <th width="30%">用户密码</th>
  32. <th width="15%">操作</th>
  33. </thead>
  34. <c:forEach items="${userlist}" var="list">
  35. <tr class="a1">
  36. <td align = "center">${list.userid}</td>
  37. <td align = "center">${list.username}</td>
  38. <td align = "center">${list.password}</td>
  39. <td align = "center"><a href="">修改</a>|<a href="delete.action?userid=${list.userid}">删除</a></td>
  40. </tr>
  41. </c:forEach>
  42. </table>
  43. <center><input type="submit" value="查看用户列表" onclick="load()"/></center>
  44. </body>
  45. </html>

15、register.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>suqiang exercise</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <link rel="stylesheet" type="text/css" href="css/styles.css">
  17. </head>
  18. <script type="text/javascript">
  19. function check(){
  20. var name = document.getElementById("username").value;
  21. var pwd = document.getElementById("password").value;
  22. var email = document.getElementById("email").value;
  23. if(name == "" || name == null){
  24. document.getElementById("tip").innerHTML="用户名不能为空哦!";
  25. return false;
  26. }
  27. if(pwd == "" || pwd == null){
  28. document.getElementById("tip").innerHTML="密码不能为空哦!";
  29. return false;
  30. }
  31. if(email == "" || email == null){
  32. document.getElementById("tip").innerHTML="邮箱必须填写,方便您找回密码。";
  33. return false;
  34. }
  35. regForm.submit();
  36. }
  37. function codefans(){
  38. var box=document.getElementById("tip");
  39. box.style.display="none";
  40. }
  41. setTimeout("codefans()",6000);//2秒改
  42. </script>
  43. <body>
  44. <form name="regForm" action="register.action" method="post">
  45. <table width="30%" id="mytab" border="1" class="t1">
  46. <tr class="a1">
  47. <th colspan="3" align="center">用户注册</th>
  48. </tr>
  49. <tr class="a1">
  50. <th width="20%">用户名</th>
  51. <td colspan="2"><input type="text" name="username" id="username"/></td>
  52. </tr>
  53. <tr class="a1">
  54. <th width="20%">密  码</th>
  55. <td colspan="2"><input type="password" name="password" id="password"/></td>
  56. </tr>
  57. <tr class="a1">
  58. <th width="20%">邮  箱</th>
  59. <td colspan="2"><input type="text" name="email" id="email"/></td>
  60. </tr>
  61. <tr class="a1">
  62. <td align="left"> </td>
  63. <td align="center"><input type="button" value="注册" onclick="check()"/></td>
  64. <td align="right"><a href="login.jsp">直接返回登录</a></td>
  65. </tr>
  66. </table>
  67. </form>
  68. <center><div style="color:#ff0000" id="tip"><h1>${info}</h1></div></center>
  69. </body>
  70. </html>

16、forgetpwd.jsp

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3. <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
  4. <%
  5. String path = request.getContextPath();
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'forgetpwd.jsp' starting page</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <link rel="stylesheet" type="text/css" href="css/styles.css">
  19. </head>
  20. <script type="text/javascript">
  21. function check(){
  22. var name = document.getElementById("username").value;
  23. if(name == "" || name == null){
  24. document.getElementById("tip").innerHTML="用户名不能为空哦!";
  25. return false;
  26. }
  27. pwdForm.submit();
  28. }
  29. function codefans(){
  30. var box=document.getElementById("tip");
  31. box.style.display="none";
  32. }
  33. setTimeout("codefans()",3000);
  34. </script>
  35. <body>
  36. <form name="pwdForm" action="forgetPwd.action" method="post">
  37. <table width="30%" id="mytab" border="1" class="t1">
  38. <tr class="a1">
  39. <th colspan="3" align="center">找回密码</th>
  40. </tr>
  41. <tr class="a1">
  42. <th width="20%">用户名</th>
  43. <td colspan="2"><input type="text" name="username" id="username"/></td>
  44. </tr>
  45. <tr class="a1">
  46. <td align="left"></td>
  47. <td align="center"><input type="button" value="下一步" onclick="check()"/></td>
  48. <td align="right"></td>
  49. </tr>
  50. </table>
  51. </form>
  52. <center><div style="color:#ff0000" id="tip"><h1>${info}</h1></div></center>
  53. </body>
  54. </html>

17,、styles.css

[css]  view plain copy
  1. @CHARSET "UTF-8";
  2. body,table{
  3. font-size:12px;
  4. }
  5. table{
  6. table-layout:fixed;
  7. empty-cells:show;
  8. border-collapse: collapse;
  9. margin:0 auto;
  10. vertical-align:middle;
  11. }
  12. td{
  13. height:20px;
  14. }
  15. h1,h2,h3{
  16. font-size:12px;
  17. margin:0;
  18. padding:0;
  19. }
  20. .title { background: #FFF; border: 1px solid #9DB3C5; padding: 1px; width:90%;margin:20px auto; }
  21. .title h1 { line-height: 31px; text-align:center; background: #2F589C url(th_bg2.gif); background-repeat: repeat-x; background-position: 0 0; color: #FFF; }
  22. .title th, .title td { border: 1px solid #CAD9EA; padding: 5px; }
  23. /*这个是借鉴一个论坛的样式*/
  24. table.t1{
  25. border:1px solid #cad9ea;
  26. color:#666;
  27. }
  28. table.t1 th {
  29. background-image: url(th_bg1.gif);
  30. background-repeat::repeat-x;
  31. height:30px;
  32. }
  33. table.t1 td,table.t1 th{
  34. border:1px solid #cad9ea;
  35. padding:0 1em 0;
  36. }
  37. table.t1 tr.a1{
  38. background-color:#f5fafe;
  39. }

小黑马1号 说 贫道望青楼相关推荐

  1. 微信公众平台小程序(应用号)开始内测了

    在今年1月的微信公开课Pro版现场,微信团队曾经提到,微信将在订阅号和服务号的基础上,推出应用号. 微信小程序正式上线 [爆]小程序内可直接打开网页了! 2017年3月27日更新:微信小程序新增六大能 ...

  2. APP、PC客户端抓包、小程序\公众号

    APP.小程序.公众号抓包 一.APP抓包 (一)BurpSuite抓取手机HTTP数据包 1.配置代理IP与端口 2.测试 (二)BurpSuite抓取手机HTTPS数据包 1.安装证书 2.测试 ...

  3. 小 V 视频号下载工具(可下载所有视频号中的视频+公众号中的部分视频、音频)

    这个视频下载软件名叫小 V 视频号下载工具,为PC版,所以得用微信PC版配合操作. 如何下载微信视频号中的视频 使用小 V 视频号下载工具来下载视频非常的简单,只需轻松两步即可下载视频. 首先我们在微 ...

  4. 开源全平台版知识付费系统源码 支持微信小程序+公众号+H5+PC端

    分享一个开源全平台版知识付费系统源码,系统支持微信小程序+公众号+H5+PC端,一套系统实现全端数据及用户体系全面打通,轻松实现店铺全网一站式运营.含完整代码包和详细搭建教程. 系统支持视频课程.音频 ...

  5. 【疯狂诗词大会小程序2.0】功能模块+前端+诗词答题小程序+内置数千道题目+开箱即用

    源码简介与安装说明: 模块介绍: 诗词答题小程序,支持单项选择题.文字线索题.看图猜诗词.读诗句猜谜等题目类型. 内置数千道题目,开箱即用.随机出题,先易后难. 诗词同步学,每一道诗题都配备了优质的诗 ...

  6. 美团饿了么外卖返利小程序公众号搭建外卖返利分销系统代cps源码

    美团饿了么外卖返利小程序公众号搭建外卖返利分销系统代cps源码 外卖CPS小程序源码分享 饿了么.美团优惠开发(外卖cps,三级裂变源码) 源码或搭建 http://y.mybei.cn/ 截图 功能 ...

  7. 北京摇号系统服务器,支付宝“城市服务”平台可查询北京小客车摇号结果

    支付宝"城市服务"平台可查询北京小客车摇号结果 [TechWeb报道]9月7日消息,北京市交通委与支付宝达成合作,市内交通出行服务将登陆城市服务平台.市民在手机上进入支付宝&quo ...

  8. 国华小状元1号年金险怎么样?好不好?

    很多家长开始为孩子做好未来的教育准备,有一些家长过来私信学姐,少儿年金险有没有必要给孩子买一个? 正巧,学姐测评了一款少儿年金险,是国华人寿旗下的--国华小状元1号少儿年金保险. 听说保障内容不错?让 ...

  9. 外卖返利小程序系统公众号外卖饿了么美团cps返利小程序分销系统

    外卖返利小程序系统公众号外卖饿了么美团cps返利小程序分销系统 外卖CPS红包小程序源码分享 外卖券外卖省省外卖探探美团饿了么外卖联盟优惠券小程序系统软件开发源码 美团/饿了么外卖CPS联盟返利公众号 ...

最新文章

  1. Centos7安装Miniconda及配置jupyter
  2. Java的知识点21——String类、StringBuffer和StringBuilder、不可变和可变字符序列使用陷阱
  3. C# 非模式窗体show()和模式窗体showdialog()的区别
  4. 自定义控件 一 创建最简单的控件
  5. 在这个功能上,iOS 落后 Android 了
  6. 叮咚买菜更新招股书:发行价区间为23.5-25.5美元
  7. 小米四曲面瀑布屏概念手机亮相:按键、开孔、边框全部消失
  8. 五子棋python设计心得_python五子棋游戏的设计与实现
  9. [数据分析工具] Pandas 不可不知的功能(一)
  10. 整理了10个行业的30份可视化大屏模板,可直接拿走套用
  11. 创建一个war类型的maven项目
  12. 使用多种算法挖掘Alexa域名数据
  13. 正交设计 python算法_正交设计 - SegmentFault 思否
  14. java中的undefined_undefined是什么意思啊?
  15. 大名鼎鼎2006 7.2版
  16. idea 创建springboot项目的资源文件application.yml的图标显示不正常
  17. c# 判断路径是否存在
  18. Debugging RJS
  19. 为什么设计思维对产品设计有帮助?
  20. 计算机图形学之纹理的作用

热门文章

  1. 同步亚马逊产品广告(SP)业务数据
  2. PHP foreach() 循环continue跳出循环用法简述
  3. 【组播技术入门 02】组播IP地址及组播MAC地址
  4. 前端web3入门脚本三:一键完成与dex的交互,羊毛党必备
  5. 查询锁定表中非锁定记录。
  6. 半夜看小说伤眼睛怎么办?
  7. waitKey()函数的一些用法
  8. android网络是否可用,android 判断网络是否可用与连接的网络是否能上网
  9. AndroidStudio中* daemon not running; starting now at tcp:5037
  10. 【Redis】部署架构 - 单节点