Project Flow
First step: Create a Java webproject
Step Two: Create three interfaces, 1,login.jsp 2 success.jsp 3 fail.jsp
The third step: Change the new Interface encoding format, Utf-8 silent encoding format will be garbled in Chinese.
Fourth step: Add the current new project to the Tomcat server and start the server to see the interface effect
Fifth Step: Browser access http://127.0.0.1:8080/HelloServlet/login.jsp 127.0.0.1 local IP 8080 port number, the Tomcat default port number is the project name plus the specified interface name
The interface has been built.
Start Focus servlet
What is a servlet?
A servlet is a class of the Java programming language that is used to extend the performance of the server, responding to any type of request, specifically www.baidu.com HHA
Servlet has two request mode get and post, our interface uses POST request way
Sixth step: The whole case has been completed, try to run it again, see the effect
The sign-in button should use the Submit
The specific reason is unclear, we debug the code ...
Java Inside string judgment equality error case username== "sa" correctly with Username.equals ("sa")
1 New Project HelloServlet
Click OK to finish
2 new Interface login.jsp success.jsp fail.jsp
Click Finish
Click Finish
Click Finish
3 Adjusting the interface
A. Since the interface default encoding format only supports English, input Chinese will be garbled, we change to Utf-8
The modified effect
PS: The three new interfaces that were just created are
4: A: Modify the login.jsp code
Add the following code inside the body
<!-- by post- <form action= "logins" method= "POST" > username: <input type= "text" Name= " Username "><br> password: <input type=" password "name=" password "><br> <input type=" Submit "> </form>
B:success.jsp interface
<div style= "size:25px;" > Log In Success </div>
C:fail.jsp interface
<div style= "size:25px;" > Log in Failure </div>
5, adding a servlet class
A, create a new package first
B New Servlet
Note: We just have to choose the Doget Dopost method.
Note: Red box code
Create a new servlet class and replace it with the following code.
PackageCom.wilson.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classLoginsextendsHttpServlet {/*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value met Hod equals to get. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request,response); } /*** The DoPost method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to post. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String username= Request.getparameter ("username"); String Password= Request.getparameter ("Password"); //because there is no database connection, write a dead judge of the user name password if(Username.equals ("sa") &&password.equals ("123")) {System.out.println ("Log In Success"); //Jump Login Success ScreenResponse.sendredirect ("success.jsp"); }Else{ //Otherwise, jump to our defined failure interface System.out.println like a log to print a bitSystem.out.println ("Log in Failed"); Response.sendredirect ("Fail.jsp"); } }}
Logins
Add to Tomcat and run Tomcat
Then the browser enters the URL: http://127.0.0.1:8080/HelloServlet/login.jsp
Enter user name password: SA 123
We're trying to enter an incorrect user name.
OK, all allowed through.
Javaweb Easy-to-start case based on Java servlet login