/* Practice Questions:
* Create a person data table in MySQL database, add three fields, Id,user,password, and enter several records
*
* Exercises: Define a login.html, which defines two request fields: User,password, sending requests to loginservlet.
* Re-create one, Loginservlet (need to inherit from HttpServlet, and override Dopost method)
* The User,password in which the request was obtained
*
* Use JDBC to query the person table for any records that correspond to the User,password of the page input
*
* If there is the corresponding hello:xxx, if not, the corresponding sorry:xxx XXX for the user
* */
--------------------------------------------------------------------------------------------------------------- ---
First build a table in MySQL with the name of my person, and then take the open source package that connects to the database.
------------------------------------------------------------------------------
The Web. xml file under LIB is configured as:
<?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 Configuration and mapping--
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.lanqiao.javatest.LoginServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<!--/loginservlet is a form submission method in Login.html-
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
-------------------------------------------------------------------------------------------
Form login.html created under WebContent, making the class, configuration file, form connected
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> How to submit the form </title>
<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>
-------------------------------------------------------------------------------------
LoginServlet1 class:
public class LoginServlet1 extends httpservlet{
HttpServlet: is a Servlet that inherits from Genericservlet. Customized for the HTTP protocol.
@Override
Overriding the Dopost method
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 the person where"
+ "Username=?" and password=? ";
Connection Connection=null;
PreparedStatement Preparedstatement=null;
ResultSet Resultset=null;
try {
Get the file that connects to the database
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 ();
}
}
}
}
}
Javaweb Small exercise: Find the same username and password in the database