Experiment 4 Java Web Programming
1, Experimental purpose
1. Master the installation and configuration of Tomcat, a Java Web server.
2. Learn simple HTML form design and form submission.
3. Master the use of request object and session object in JSP.
2, Experimental content
1. Download and install Tomcat8 and configure it. Start the Tomcat server and enter in the browser http://127.0.0.1:8080/ , view the browser output interface;
2. Customized configuration:
(1) Modify the Tomcat service port to 88 (the default installation is 8080);
(2) Modify the character encoding of HTTP GET mode to UTF-8 (ISO-8859-1 by default);
3. Write a user registration module with the following functions:
(1) Design the user registration form page register.jsp, including: user name (text box), password (password box), gender (drop-down box);
(2) The design user registration data saving page doRegister.jsp is used to save user data to the user.txt file.
Save one user in each line of user.txt in the format of "user name, password, gender";
(3) If user.txt already contains a user with the same name, you will be prompted that the user already exists; Otherwise, you will be prompted that the registration is successful.
4. Write the user login module with the following functions:
(1) Design the user login form page login.jsp, including: user name (text box) and password (password box);
(2) Design the login authentication file doLogin.jsp, obtain the user name and password submitted by the client, and then verify whether the user exists and whether the login password is correct from user.txt;
(3) Feed back the login verification results to the user.
3, Thinking questions (in this article)
1. Summarize the whole experiment and write out the experimental experience;
2. For those who have spare power, replace the data source user.txt of the registration and login function in the experiment with the user table of the database (self-designed field), and use JDBC to access the database;
First, install Apache Tomcat 8 or above and start the local server
Of course, jsp pages do not have to be started by Eclipse ides. In fact, they can be started by Notepad
Installation tutorial:
Tomcat installation and configuration tutorial - full stack developer community - blog Park
I choose the Eclipse IDE for java developers. If I want to create a web project in it, I need to download the corresponding plug-in first. Tutorial:
New web project not found under eclipse_ tangju7086 blog - CSDN blog_ Eclipse has no web
MySQL version 8.0.25 is selected for the database. Installation tutorial:
It is not recommended that you use the command line to operate the database. You can choose to download Mysql Workbench and install the tutorial:
Note: if you want to use MySQL in a web project, you must not forget to reposition the MySQL driver jar package under the lib directory of Tomcat:
Then create a database named user1 in mysql, including a user table:
Login interface jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%--You can add references here--%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>mysql Login interface for</title> </head> <body> <form action="./logincheck1.jsp" method="post"> <%--form Statement - when the form is submitted, the data is sent, and a link relationship is generated here---%> <div align="center"><h1><b>User login</b></h1> <p> <strong><font size="5">Sign in id: </font></strong> <input type="text" name="userid" style="width:300px;height:20px;font:6px"/> <%--type It should be the type of label, name Then click cover face, as if you can also specify the initial value value=--%> </p> <p> <strong><font size="5">password:</font></strong> <input type="password" name="password" style="width:300px;height:20px;font:6px"/></p> <input type="submit" value="determine" style="width:100px;height:25px"/> <input type="reset" value="cancel" style="width:100px;height:25px"/> <select name="sex" style="width:100px;height:25px";font="6px"> <option value="female">female</option> <option value="male">male</option> </select> <input type = "button" value = "register" onclick = "window.location.href = './doRegister.jsp'"> </div></form> </body> </html>
Login detection interface:
<%@page import="java.io.FileInputStream"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*" %> <%@ page import="javax.servlet.*" %> <%@ page import="java.io.*" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Server Page Depend !</title> </head> <body> <h3>Which Pae will be depend by the user's message!</h3> <% request.setCharacterEncoding("UTF-8"); String name=request.getParameter("userid");//Get the parameter value of name String password=request.getParameter("password");//Get the parameter value of password String sex=request.getParameter("sex");//Get the parameter value of gender %> <%! public static Connection conn; private static Properties prop1; public static String Driver1; public static String url1; public static String user1; public static String password1; public static PreparedStatement stmt; public ResultSet rs; %> <% try{ prop1=new Properties(); prop1.load(new FileInputStream(new File("c:/java5/mysqlinfo.properties"))); //This should be the location of the configuration file on your computer Driver1=prop1.getProperty("driver"); url1=prop1.getProperty("url"); user1=prop1.getProperty("user"); password1=prop1.getProperty("pass"); Class.forName(Driver1);//Load mysql driver conn=DriverManager.getConnection(url1,user1,password1); //localhost is the local address, 3306 is the port number, and finally the user name and password stmt=conn.prepareStatement("SELECT * from user where username='"+name+"' and password='"+password+"' and sex='"+sex+"'");//Instantiate the Statement object rs=stmt.executeQuery();//Perform a database query operation and get the result set boolean flag=false;//Initialize flag if(rs.next()){//If there is more than one judgment result, it proves that the user password pair exists flag=true; session.setAttribute("UserName", name);//Assign the content of name to UserName }else{ flag=false; } %> <% if(flag){ %> <jsp:forward page="./login_success.jsp"> <jsp:param name="uname" value="<%=name%>"/> </jsp:forward> <% }else{ %> <jsp:forward page="./login_failure.jsp"> <jsp:param name="uname" value="<%=name%>"/> </jsp:forward> <% } }catch(Exception excp1){excp1.printStackTrace();} finally{ //Close the above objects. Pay attention to the closing sequence. The last one is closed first rs.close(); stmt.close(); conn.close(); } %> </body> </html>
Login / registration successful interface jsp:
<%@page import="java.util.Date"%> <%@page import="javax.swing.Timer"%> <%@page import="java.lang.*" %> <%@page import="java.text.*" %> <%@page import="java.lang.Exception" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Sign in/login was successful</title> </head> <body><div align="center"> <p> <h1><strong>User login/login was successful!!!</strong></h1> <h2>Current time:<strong> <%out.println(new SimpleDateFormat("hh:mm:ss").format(new Date()));%> </strong></h2> <h3><a href="./doRegister.jsp">Jump to the registration interface</a></h3> </div></body> </html>
Login failure interface jsp:
<%@page import="java.util.Date"%> <%@page import="javax.swing.Timer"%> <%@page import="java.lang.*" %> <%@page import="java.text.*" %> <%@page import="java.lang.Exception" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login failed</title> </head> <body><div align="center"> <p> <h1><strong>User registration/Login failed!</strong></h1> <h2>Current time:<strong><% out.println(new SimpleDateFormat("hh:mm:ss").format(new Date())); %></strong></h2> <h3><a href="./register31.jsp">Jump to login screen</a></h3> <h3><a href="./doRegister.jsp">Jump to the registration interface</a></h3> </div></body> </h
Registration interface jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%--You can add references here--%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>mysql User registration interface</title> </head> <body> <form action="./conduct register.jsp" method="post"> <%--form Statement - when the form is submitted, the data is sent, and a link relationship is generated here---%> <div align="center"><h1><b>User registration</b></h1> <p> <strong><font size="5">user id: </font></strong> <input type="text" name="userid" style="width:300px;height:20px;font:6px"/> <%--type Should be the specified input type, name Then click cover face, as if you can also specify the initial value value=--%> </p> <p> <strong><font size="5">User password:</font></strong> <input type="password" name="password" style="width:300px;height:20px;font:6px"/></p> <input type="submit" value="determine" style="width:100px;height:25px"/> <input type="reset" value="cancel" style="width:100px;height:25px"/> <select name="sex" style="width:100px;height:25px";font="6px"> <option value="female">female</option> <option value="male">male</option> </select> </div></form> </body> </html>
Registration detection (whether to register duplicate user names) interface jsp:
<%@page import="java.io.FileInputStream"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*" %> <%@ page import="javax.servlet.*" %> <%@ page import="java.io.*" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Server Page Depend !</title> </head> <body> <h3>Which Pae will be depend by the user's message!</h3> <% request.setCharacterEncoding("UTF-8"); String name=request.getParameter("userid");//Get the parameter value of name String password=request.getParameter("password");//Get the parameter value of password String sex=request.getParameter("sex"); %> <%! public static Connection conn; private static Properties prop1; public static String Driver1; public static String url1; public static String user1; public static String password1; public static PreparedStatement stmt,stmt1; public ResultSet rs,rs1; %> <% try{ prop1=new Properties();//Read the configuration file and initialize the database connection information prop1.load(new FileInputStream(new File("c:/java5/mysqlinfo.properties"))); Driver1=prop1.getProperty("driver"); url1=prop1.getProperty("url"); user1=prop1.getProperty("user"); password1=prop1.getProperty("pass"); Class.forName(Driver1);//When loading the mysql driver, remember to put the jar of the driver under the lib directory of tomcat conn=DriverManager.getConnection(url1,user1,password1); stmt1=conn.prepareStatement("SELECT * FROM user WHERE username='"+name+"'"); stmt=conn.prepareStatement("INSERT INTO user(username, password,sex) VALUES ('"+name+"', '"+password+"', '"+sex+"')");//Instantiate the PreparedStatement object rs1=stmt1.executeQuery(); if(rs1.next()){//First judge whether the name is duplicate %> <jsp:forward page="./register_failure.jsp"></jsp:forward> <!--If there are no elements in the tag, you cannot wrap [cover face], and the execution will not continue here--> <% }else{ stmt.executeUpdate();//Perform database insert operation %> <jsp:forward page="./login_success.jsp"></jsp:forward> <% } }catch(Exception excp1){excp1.printStackTrace();} finally{//Close the above objects. Pay attention to the closing sequence. The last one is closed first stmt1.close(); stmt.close(); conn.close(); } %> </body> </html>
Registration failed (duplicate user name) interface jsp:
<%@page import="java.util.Date"%> <%@page import="javax.swing.Timer"%> <%@page import="java.lang.*" %> <%@page import="java.text.*" %> <%@page import="java.lang.Exception" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>login has failed</title> </head> <body><div align="center"> <p> <h1><strong>Duplicate user name, please re register!</strong></h1> <h2>Current time:<strong><% out.println(new SimpleDateFormat("hh:mm:ss").format(new Date())); %></strong></h2> <h3><a href="./register31.jsp">Jump to login screen</a></h3> <h3><a href="./doRegister.jsp">Jump to the registration interface</a></h3> </div></body> </html>
Login Demo:
Enter user name: hhh, password: 123456, gender: Female
Information in the database:
result:
Registration demonstration:
User name: hhhy6 Password: 123abc gender: Female
After registration, the database information:
Registration failed:
Register another user name: hhhy6 Password: whatever Gender: Female
Obviously, it had a duplicate name, so it failed——