当前位置: 代码迷 >> Web前端 >> JavaWeb快速入门8(后附源码)
  详细解决方案

JavaWeb快速入门8(后附源码)

热度:663   发布时间:2012-10-13 11:38:17.0
JavaWeb快速入门八(后附源码)

优化

?

test2的基础上做修改。

创建util包,在utils包中创建JdbcUtils.java工具类。为项目提供数据库连接和释放资源。内容如下:

JdbcUtils.java

package util;

?

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

?

public class JdbcUtils {

?

??? //mysql数据库连接协议;mytest为数据库名称

??? private static String url = "jdbc:mysql://localhost:3306/mytest";

??? private static String user = "root";//mysql数据库用户名

??? private static String password = "root";//mysql的密码

?

??? private JdbcUtils() {

??? }

?

??? // 加载驱动类

??? static {

?????? try {

?????????? Class.forName("com.mysql.jdbc.Driver");

?????? } catch (ClassNotFoundException e) {

?????????? throw new ExceptionInInitializerError(e);

?????? }

??? }

?

??? // 得到连接

??? public static? Connection getConnection() throws SQLException {

?????? return DriverManager.getConnection(url, user, password);

??? }

?

??? // 释放资源

??? public static void free(ResultSet rs, Statement st, Connection conn) {

?????? try {

?????????? if (rs != null) {

????????????? rs.close();

?????????? }

?????? } catch (SQLException e) {

?????????? e.printStackTrace();

?????? } finally {

?????????? try {

????????????? if (st != null) {

????????????????? st.close();

????????????? }

?????????? } catch (SQLException e) {

????????????? e.printStackTrace();

?????????? } finally {

????????????? try {

????????????????? if (conn != null) {

???????????????????? conn.close();

????????????????? }

????????????? } catch (SQLException e) {

????????????????? e.printStackTrace();

????????????? }

?????????? }

?????? }

??? }

}

?

前面我们写了一个注册的例子,现在我们在前面基础上写一个登陆的类。创建LoginServlet.java,处理登陆操作。内容如下:

LoginServlet.java

package servlet;

?

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

?

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

?

import util.JdbcUtils;

?

public class LoginServlet extends HttpServlet {

?

??? public void doGet(HttpServletRequest request, HttpServletResponse response)

?????????? throws ServletException, IOException {

?????? doPost(request,response);

??? }

?

??? public void doPost(HttpServletRequest request, HttpServletResponse response)

?????????? throws ServletException, IOException {

?????? String registerName = request.getParameter("name");

?????? String registerPassword = request.getParameter("password");

??????

?????? Connection conn = null;

?????? PreparedStatement ps = null;

?????? ResultSet rs = null;

?????? try {

?????????? conn = JdbcUtils.getConnection();

?????????? String sql = "select name,password from user where name=? and password=?";

?????????? ps = conn.prepareStatement(sql);

?????????? ps.setString(1, registerName);

?????????? ps.setString(2, registerPassword);

?????????? rs = ps.executeQuery();

?????????? if(rs.next()){

????????????? String name = rs.getString(1);

????????????? String password = rs.getString(2);

????????????? System.out.println(name+password);

??? ?????????? request.setAttribute("name", name);// 将从数据库获得名字和密码setrequest对象里

????????????? request.setAttribute("password", password);

????????????? request.getRequestDispatcher("/login_success.jsp").forward(request,response);

?????????? }else{

????????????? request.getRequestDispatcher("/login_fail.jsp").forward(request,response);

?????????? }

??????????

?????? } catch (SQLException e) {

?????????? e.printStackTrace();

?????? }finally{

?????????? JdbcUtils.free(rs, ps, conn);

?????? }

??? }

?

}

?

新建三个jsp页面――login.jsp 登陆页面;login_success.jsp登陆成功页面;login_fail.jsp登陆失败页面

?

login.jsp 内容如下:

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? <head>

? </head>

? <body>

??? <form action="servlet/LoginServlet" method="post">

??? Plase write your name and password!<br/>

??? name:<input type="text" name="name"/><br/>

??? password:<input type="password" name="password"/>

??? <input type="submit" value="login"/>

??? </form>

? </body>

</html>

?

login_success.jsp 内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? <body>

??? <font color="red">login success!</font><br/>

??? <font color="blue">Your name is :</font><%=request.getAttribute("name") %><br/>

??? <font color="blue">Your password is :</font><%=request.getAttribute("password") %>

? </body>

</html>

?

login_fail.jsp 内容如下:

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? <body>

??? <font color="red">login fail!</font><br/>

? </body>

</html>

?

测试地址:http://localhost:8080/test2/login.jsp

?