Learning Objectives:
- Experiment name: JSP built-in object
- Objective: To master all the syntax of JSP pages. Able to write basic JSP web pages.
Learning content:
1. Experiment
Location: Unit 2 on Wednesday, 10617 Comprehensive Laboratory 1, bring your own computer
Purpose: Master the main usage methods of various built-in objects, and be able to use built-in objects to complete specific functions.
Content:
◆(1) Improve the registration function, and save the user registration information in the application after successful registration. (2) Improve the login function, compare and verify the user login information with the one stored in the application, if the login is successful, enter the home page and save the account into the session, if the login fails, return to the login page.
Requirements:
Start the experiment before the experiment class
>In the last half-hour of the experimental class, the team leader organizes a group demonstration and acceptance, mutual evaluation and scoring, and the team leader sends the final result to the teacher
◆Write the experiment report this time and upload the learning pass
Scoring criteria for group mutual evaluation in experimental class:
90-100 points: The experiment is completed and the operation effect is good.
80-89 points: The experiment is basically completed or there are defects, and the operation effect is average. 70-79 points: More than half of the experiment has been carried out.
60-69 : Less than half of the experiment was carried out.
Below 60 points: still learning to digest, no experiments have been started.
Learning time:
- Monday to Friday 7:00pm – 9:00pm
- Saturday 9am-11am
- Sunday 3pm-6pm
Learning outputs:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <body> <h2>Welcome to the main page!</h2> <% request.setCharacterEncoding("utf-8"); String username = request.getSession().getAttribute("username").toString(); %> <h3 style="color: red">Hello, <%=username %></h3> </body> </html>
login.jsp
<%-- Created by IntelliJ IDEA. User: baizhimin Date: 2023/3/23 Time: 22:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login Page</title> </head> <body> <h1>Login page</h1> <form action="login_deal.jsp" method="POST"> <label for="username">Username:</label> <input type="text" id="username" name="username" required> <br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required> <br><br> <input type="submit" value="login"> </form> <br> <p>Don't have an account? <a href="register.jsp">Click to register</a></p> </body> </html>
login_deal.jsp
<%@ page import="java.util.HashMap" %><%-- Created by IntelliJ IDEA. User: baizhimin Date: 2023/3/23 Time: 22:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login_deal</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String path = request. getContextPath(); String username = request. getParameter("username"); String password = request. getParameter("password"); request.getSession().setAttribute("username",username); HashMap<String,String> userMap = (HashMap<String,String>)application.getAttribute("userMap"); if(userMap==null){ out.print("Login failed! Will return to login interface"); response.setHeader("refresh","2;url=" + path + "/login.jsp"); }else{ String pwd = userMap. get(username); if(pwd!=null & amp; & amp; pwd. equals(password)){ session.setAttribute("username", username); out.print("Successful login! Will return to the main page soon"); response.setHeader("refresh","2;url=" + path + "/index.jsp"); }else{ out.print("Login failed! Will return to login interface"); response.setHeader("refresh","2;url=" + path + "/login.jsp"); } } %> </body> </html>
register.jsp
<%-- Created by IntelliJ IDEA. User: baizhimin Date: 2023/3/23 Time: 22:08 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Registration Page</title> </head> <body> <h1>Registration page</h1> <form action="register_deal.jsp" method="POST"> <label for="username">Username:</label> <input type="text" id="username" name="username" required> <br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required> <br><br> <input type="submit" value="register" onclick="alert('registration successful')"> </form> <br> <p>Already have an account? <a href="login.jsp">Click to log in</a></p> </body> </html>
register_deal.jsp
<%@ page import="java.util.HashMap" %><%-- Created by IntelliJ IDEA. User: baizhimin Date: 2023/3/23 Time: 22:09 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>register_deal</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String path = request. getContextPath(); String username = request. getParameter("username"); String password = request. getParameter("password"); request.getSession().setAttribute("username",username); HashMap<String,String> userMap = (HashMap<String,String>)application.getAttribute("userMap"); if(userMap==null){ userMap = new HashMap<String,String>(); userMap. put(username, password); application.setAttribute("userMap",userMap); out.print("Successful registration! Will return to the main page soon"); response.setHeader("refresh","2;url=" + path + "/index.jsp"); }else{ if(userMap. containsKey(username)){ out.print("Account already exists! Will return to the registration page soon"); response.setHeader("refresh","2;url=" + path + "/register.jsp"); }else{ userMap. put(username, password); out.print("Successful registration! Will return to the main page soon"); response.setHeader("refresh","2;url=" + path + "/index.jsp"); } } %> </body> </html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>WebTest5</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--jsp--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> </dependencies> <packaging>war</packaging> <!--servlet--> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <!--Project access path. The currently configured access is localhost:9090/, if the configuration is /aa, the access path is localhost:9090/aa --> <port>9090</port> <!-- <url>http://localhost:9090/register.jsp</url>--> <uriEncoding>UTF-8</uriEncoding><!-- optional --> </configuration> </plugin> </plugins> </build> </project>
web.xml (set start page)
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Archetype Created Web Application</display-name> </web-app>
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108658 people are studying systematically