javaWEB小練習:在數據庫中查找相同的username和password

/*練習題:
* 在Mysql數據庫中建立一個person數據表,添加三個字段,id,user,password,並錄入幾條記錄
*
*練習題:定義一個login.html,裏面定義了兩個請求字段:user,password,發送請求到loginServlet。
*再建立一個,LoginServlet(須要繼承自HttpServlet,並重寫doPost方法)
*在其中獲取請求的user,password
*
*利用jdbc從person表中查詢有沒有和頁面輸入的user,password對應的記錄
*
*如有則相應Hello:XXX,若沒有,相應sorry:XXX XXX 爲user
* */html

------------------------------------------------------------------------------------------------------------------java

首先在mysql中創建一個表名我person的表,而後再將鏈接數據庫的開源包導進去mysql

------------------------------------------------------------------------------web

在lib下面的web.xml文件配置爲:sql

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<!-- loginServlet的配置和映射 -->
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.lanqiao.javatest.LoginServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<!-- /loginServlet是login.html中表單的提交方法 -->
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>數據庫


</web-app>app

-------------------------------------------------------------------------------------------ide

在WebContent下面創建的表單login.html,使類,配置文件,表單創建起來了鏈接post

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>表單的提交方法</title>
</head>
<body>
<form action="loginServlet" method="post">
<p>
username:<input type="text" name="username"/>
password:<input type="password" name="password"/>
<br><br>
<input type="submit" value="Submit"/>
</p>
</form>
</body>
</html>ui

-------------------------------------------------------------------------------------

LoginServlet1類:

public class LoginServlet1 extends HttpServlet{


//HttpServlet: 是一個 Servlet, 繼承自 GenericServlet. 針對於 HTTP 協議所定製.


@Override
//重寫doPost方法
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out=resp.getWriter();

String username1=req.getParameter("username");
String password1=req.getParameter("password");

String sql="select count(id) from person where "
+ "username=? and password=?";
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
//獲取鏈接數據庫的文件
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql:///test";
String username2="root";
String password2="lxn123";

connection=DriverManager.getConnection(url, username2, password2);
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, username1);
preparedStatement.setString(2, password1);
resultSet=preparedStatement.executeQuery();

if(resultSet.next()){
int count=resultSet.getInt(1);
if (count>0) {
out.println("Hello:"+username1);
}
else{
out.println("Sorry:"+username1);
}

}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

相關文章
相關標籤/搜索