本文主要是介绍【javaweb:Servlet】一个用于校验登录信息的servlet demonstration,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.servlet程序如下
package indi.login;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 indi.domain.User;
import indi.utils.C3P0Utils;
/*** 创建一个用于登录校验的servlet* @author Administrator**/
public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1.从resquest获取登录网页界面提交的用户名和密码String username = (String) request.getParameter("username");String password=(String) request.getParameter("password");//2.从数据库中查询指定用户名和密码的用户//采用C3P0工具类获取datasourceQueryRunner queryRunner=new QueryRunner(C3P0Utils.getDataSource());String sql="select * from user where uname=? and upassword=?";User user=null;try {user=queryRunner.query(sql,new BeanHandler<User>(User.class),username,password);} catch (SQLException e) {throw new RuntimeException(e);}//3.判断查询的结果是否为空if(user!=null) {response.getWriter().write(user.toString());}else {response.getWriter().write("sorry! your name or password is wrong!");}}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
2.html文件如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录提交界面</title>
</head>
<body><form action="/WEB13_Servlet/login" method="post"><input type="text" name="username"><br><input type="password" name="password"><br><input type="submit" value="提交"></form>
</body>
</html>
3.C3P0连接池配置文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///web13</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">5</property><property name="maxPoolSize">20</property></default-config><named-config name="secondname"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///web08</property><property name="user">root</property><property name="password">root</property></named-config></c3p0-config>
4.需要用到的类库
这篇关于【javaweb:Servlet】一个用于校验登录信息的servlet demonstration的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!