最近在学Javaweb的内容,看过视频后,想通过自己写一个webproject锻炼一下学到的一些知识,把做的经验总结下,希望和大家一块进步。
实现功能:完成登录验证,即验证前端提交的表单(username和password)是否在数据库中。
1.首先,前端网页代码:
Insert title here
<!--input属性:type:text文本框/password密码框/radio:单选框/checkbox:多选框-->
<!--name;要想将信息保存到服务器必须要有neme属性;能将多个单选框组合-->
<!--对于单选框,复选框需要设置value值才能传递-->
姓名:<input type="text" id="username" name="username" /><br /> 密码:<input
type="password" id="password" name="password" /><br /> <input
type="submit" value="保存" /> <input type="reset" />
</form>
2.其次servlet的内容:
package com.yj.login;
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.yj.domain.User;
import com.yj.service.Service;
/**
* 此次通过servlet操作数据库的过程中:首先需要将用到的jar包放入tomcat的lib目录下
* 操作数据库常用的有3类jar包,即commons-dbcp1.4,commons-dbutlis1.6,commons-pool1.5,
* 一般如noclassfounderror是因为缺少相应的包,若程序中导入了,tomcat安装目录中的lib或common文件夹下的lib中也应导入
*/
/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/* *//**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
Service service = new Service();
User user=new User();/*在其他包中引入了User的javabean对象,包括username和password属性*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = null;
user = service.login(username, password);
if ( user == null) {
response.getWriter().write("登录失败");
} else {
response.getWriter().write("欢迎回来");
response.getWriter().println("已提交表单" + username + password);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
3.数据库操作
3.1引入了一个JDBCUtils工具类
package com.yj.tools;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 获取数据库连接的工具类
* 实现连接池
*
*/
public class JDBCUtils {
//创建BasicDataSource对象
private static BasicDataSource datasource =new BasicDataSource();
//静态代码块,实现必要参数配置
static{
datasource.setDriverClassName(“com.mysql.jdbc.Driver”);
datasource.setUrl(“jdbc:mysql://localhost:3306/login”);
datasource.setUsername(“root”);
datasource.setPassword(“12369874”);
datasource.setMaxActive(10);
datasource.setMaxIdle(5);
datasource.setMinIdle(2);
datasource.setInitialSize(10);
}
public static DataSource getDataSource(){
return datasource;
}
}
3.2通过工具类获得datasource对象,在LoginQuery中得到该对象操作数据库
package com.yj.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.yj.domain.User;
import com.yj.tools.JDBCUtils;
public class LoginQuery {
private QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
public User login(String username, String password) {
try {
String sql="select * from user where username=? and password=?";
User user= qr.query(sql, new BeanHandler<>(User.class), username,password);
return user;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询失败");
}
}
}
总结:数据库操作是最麻烦的一步,最容易出错的地方,当然也是目前水平有限。在数据库操作中,要导入对应的包,不能把包导错。我用到的是这三个commons-dbcp1.4,commons-dbutlis1.6,commons-pool1.5,不管你用什么包,一定要把jar包在考到tomcat的安装目录lib下和commons的lib下(具体哪个我没试,可能考到一个目录下就可以了,我把两个都考了。)