liangoogle

liangoogle
随笔 - 9, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

jsp-servlet简单登陆界面 有数据库连接

使用mysql数据库
使用tomcat服务器
数据库连接类:
package com.servlet;
import java.sql.*;
public class conn {
public static String name;
public static String mima;
public conn(String name,String mima){
conn.name =name;
conn.mima=mima;
    // 1. 注册驱动
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch(ClassNotFoundException ex) {
        ex.printStackTrace();
    }
}
    public ResultSet date() {
     
        // 声明变量,使用,而后关闭
        Connection conn = null;        //数据库连接
        Statement stmt = null;         //数据库表达式
        ResultSet rs = null;             //结果集
        
        try {
            //2. 获取数据库的连接
            conn = DriverManager.getConnection
                ("jdbc:mysql://localhost:3306/dl","root","");
            
            //3. 获取表达式
            stmt = conn.createStatement();
            
            //4. 执行SQL
            String sql =  "select * from login where user='" + name
            + "' and pass = '" + mima + "'";
            rs = stmt.executeQuery(sql);
            
            //5. 现实结果集里面的数据
            //5. 现实结果集里面的数据
         //   while(rs.next()) {
             //   System.out.println("id为123的time值=" + rs.getString(1));
        //    }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
     
        finally {
           
        }
return rs;
    }
}
HttpServlet类:
package com.servlet;

import java.io.IOException;
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 com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
import com.sun.xml.internal.bind.v2.runtime.Name;


/**
 * @author Administrator
 *
 */
public class login extends HttpServlet {

/**
*/
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String name=req.getParameter("name");
String mima=req.getParameter("mima");
conn conn=new conn(name ,mima);
ResultSet rs=conn.date();
try {
if (rs.next())
  {
resp.sendRedirect("sucessed.jsp?name="+name);
  } else
  //否则登录失败
  {
    resp.sendRedirect("index.jsp");
  }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.servlet.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/jump.jsp</url-pattern>
</servlet-mapping>
</web-app>
index.php:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

系 统 登 录

<form action="../dl/jump.jsp" method="post">


用户名
<input type="text" name="name">
<br>

密&nbsp;&nbsp;&nbsp;&nbsp;码
<input type="password" name="mima" />

<br>
<input type="submit" value="登录">

<input type="reset" value="取消">
</form>

</body>
</html>
sucessed.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>

您好:
<%String name = request.getParameter("name");
out.print(name);
%>
</body>
</html>

posted on 2011-04-28 19:58 haojinlian 阅读(5571) 评论(2)  编辑  收藏

评论

# re: jsp-servlet简单登陆界面 有数据库连接[未登录]  回复  更多评论   

这个怎么破
2013-11-12 22:47 | jack

# re: jsp-servlet简单登陆界面 有数据库连接  回复  更多评论   

gjgjcvhgjjbhjkhjkh
2015-04-21 08:10 | trtu7u

只有注册用户登录后才能发表评论。


网站导航: