Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 提交代码 #655

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902121/Datab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package javabean;
import java.sql.*;
public class Datab {
boolean bInited = false;
public void initJDBC() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
bInited = true;
System.out.println("Operation succeeded!");
}
public Connection getConnection() throws ClassNotFoundException,
SQLException{
if(!bInited){
initJDBC();
}
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/user","root","");
return conn;
}
public boolean loginSuccess(String userName,String password){
boolean returnValue = false;
String sql = "SELECT * FROM user";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
String userNameInDB = rs.getString("name");
String passwordInDB = rs.getString("pwd");
if(userNameInDB.equals(userName) &&
passwordInDB.equals(password)){
returnValue = true;
break;
}
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
}
39 changes: 39 additions & 0 deletions jweb/src/edu/hzu/javaweb/labs/se1414080902121/UseServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.hzu.javaweb.labs.se1414080902121;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javabean.Datab;
public class UseServlet implements javax.servlet.Servlet{
public void destroy() {
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String userName = request.getParameter("username");
String password = request.getParameter("password");
Datab db = new Datab();
boolean canLogin = db.loginSuccess(userName, password);
if(canLogin){
response.sendRedirect("display.jsp");
}else{
response.sendRedirect("failure.jsp");
}
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
HttpServletRequest rq = (HttpServletRequest)request;
HttpServletResponse rs = (HttpServletResponse) response;
doPost(rq,rs);
}
}
42 changes: 42 additions & 0 deletions jweb/web/1414080902121/login.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆</title>
<style>
form{width:350px;height:200px;}
form fieldset{border:0;align:center;}
form label{display:inline-block;width:60px;text-align:right}
input{width:300px;height:35px;}
#submit{width:200px;height:35px;position:relative;left:60px;}
#submit:hover{
cursor: pointer;
background: #FDF5E6;
}
body{
width:100%;
height:100%;
background: #CCDDFF;
}
#box{position:fixed;left:0px;right:0px;width:606px;margin-left:auto;margin-right:auto;top:100px;}
a{position:relative;bottom:-10px;}
</style>
</head>
<body>
<div id="box">
<form action="login" style="background:#cccccc" method="post">
<fieldset>
<p>
<label for="user">用户:</label>
<input type="text" class="" id="user" name="username" placeholder="user">
<br>
<label for="password">密码:</label>
<input type="password" class="" id="password" name="password" placeholder="password">
<button type="submit" id="submit">登录</button>
</fieldset>
</form>
</div>
</body>
</html>