JavaWeb基础之Servlet简单实现用户登陆
学习javaweb遇到了一些坑,一些问题总结下来,记个笔记。
学习servlet遇到的一些坑:
servlet实现用户登陆遇到的坑解决办法:
https://www.cnblogs.com/swxj/archive/2013/06/05/3119206.html
打开配置选择server runtime envir-->绑定运行环境
tomcat创建要自定义值
导入tomcat自带的jar包:项目右键--->build path-->add library-->选择Server Runtime--->选择tomcat服务器,这样就能自动导入所有tomcat下的jar包了!无需再二次导入。
使用Servlet简单实现用户登陆功能
除了这些jar包另外还需要二次导入jar包:c3p0-* jar包+commons-dbutils-*jar包
首先创建一张表用于测试用户登陆:
User表:
/*Mysql数据库*/ create table user( username varchar(20), password varchar(30), id int );
创建设置公共的servlet模块
1.套servlet模块直接调用模块
配置servlet加载项
eclipse里面搜索templates 找到java-->editor--->templates然后new
公共的模块代码:
设置模块名称servlet,创建servlet之后直接ser+alt+/自动补全就会全部代码结构就位。
package ${enclosing_package}; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ${primary_type_name} extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("hello world!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
因为用到c3p0jar包所以固定模式下的c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://127.0.0.1/数据库名</property> <property name="user">数据库名</property> <property name="password">数据库密码</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="test"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://127.0.0.1/数据库名</property> <property name="user">数据库用户名</property> <property name="password">数据库密码</property> </named-config> </c3p0-config>
用到commons-dbutils-*jar包,需要一个数据库dbutilsl类
创建包名:cn.test.utils,文件名:DataSourceUtils.java
内容如下:
package cn.test.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceUtils { private static DataSource dataSource = new ComboPooledDataSource(); private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); // 直接可以获取一个连接池 public static DataSource getDataSource() { return dataSource; } // 获取连接对象 public static Connection getConnection() throws SQLException { Connection con = tl.get(); if (con == null) { con = dataSource.getConnection(); tl.set(con); } return con; } // 开启事务 public static void startTransaction() throws SQLException { Connection con = getConnection(); if (con != null) { con.setAutoCommit(false); } } // 事务回滚 public static void rollback() throws SQLException { Connection con = getConnection(); if (con != null) { con.rollback(); } } // 提交并且 关闭资源及从ThreadLocall中释放 public static void commitAndRelease() throws SQLException { Connection con = getConnection(); if (con != null) { con.commit(); // 事务提交 con.close();// 关闭资源 tl.remove();// 从线程绑定中移除 } } // 关闭资源方法 public static void closeConnection() throws SQLException { Connection con = getConnection(); if (con != null) { con.close(); } } public static void closeStatement(Statement st) throws SQLException { if (st != null) { st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } } }
创建User的用户类: cn.test.domain->User.java
package cn.test.domain; //创建一个信息类 public class User { private String username; private String password; private int id; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", id=" + id + "]"; } }
创建需要登陆的表单html视图页面:WebContent-->login.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/TestPro/login" method="post"> 账号:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> <input type="submit" value="登陆"> </form> </body> </html>
设置我们的login servlet路径
cn.test.loginServlet--->LoginServlet.java 设置访问路径是login
package cn.test.loginServlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import com.mchange.*; import cn.test.domain.User; import cn.test.utils.DataSourceUtils; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from user where username=? and password=?"; User user = null; try { user = queryRunner.query(sql, new BeanHandler<User>(User.class), username, password); // System.out.println(user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (user != null) { response.getWriter().write(user.toString()); } else { response.getWriter().write("sorry you username or password is Error!"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
访问http://localhost:对应端口/项目名称/login.html即可进行测试!