要求:
在首页面index.html有登录、注册的按钮;
1、点击登录,进入登录页面,填写信息(用户名、密码)后,点击 Login(登录)显示欢迎用户,并下方带有 Log out(退出登录)链接,点击退出登录,返回登录页面;
2、点击注册,进入注册页面,填写信息(用户名、密码、邮箱、电话)后,点击 Register(注册),显示欢迎用户,并下方带有 Download(下载)链接,点击下载,会下载用户刚刚注册的信息(除去密码)。
1、编写index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Login.html">
<table border="0">
<tr>
<td style="text-align: center"><input type="submit" value="Login"/></td>
</tr>
</table>
</form>
<form action="Register.html">
<table border="0">
<tr>
<td style="text-align: center"><input type="submit" value="Register"/></td>
</tr>
</table>
</form>
</body>
</html>
运行页面如下:
2、Login.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Login Page</h1>
<form action="login">
<table border="0">
<tr>
<td>Name:</td>
<td><input type="text" name="username" value="" placeholder="Please enter your name" required/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" value="" placeholder="Please enter your password" required/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
运行页面如下:
3、Register.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<H1>Registration Page</H1>
<form action="register">
<table border="0">
<tr>
<td>
Enter Your Name:
</td>
<td>
<input type="text" name="txtname" required>
</td>
</tr>
<tr>
<td>
Enter Your Password:
</td>
<td>
<input type="password" name="password" value="" placeholder="Please enter your password" required/>
</td>
</tr>
<tr>
<td>
Enter Your Email:
</td>
<td>
<input type="email" name="email">
</td>
</tr>
<tr>
<td>
Enter Your Phone:
</td>
<td>
<input type="tel" name="phone">
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form>
</body>
</html>
运行页面如下:
4、userpkg包里面的LoginServlet.java
(其中 LoginServlet.java 的 url 为 /login)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package userpkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author 小罗
*/
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
ServletContext ctx = getServletContext();
ctx.setAttribute("userName", name);
response.sendRedirect("Lowelcome.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
5、Lowelcome.jsp
<%--
Document : Lowelcome
Created on : 2024-4-19, 20:17:44
Author : 小罗
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<%
ServletContext ctx = getServletContext();
String name = (String)ctx.getAttribute("userName");
out.println("<h1>Welcome "+name+" !</h1>");
%>
</head>
<body>
<a href="Login.html">Log out</a>
</body>
</html>
最后填写完信息,点击登录,显示页面如下:
点击退出登录链接会天跳转回登录页面 。
6、userpkg包里面的RegisterServlet.java
(其中 RegisterServlet.java 的 url 为 /register)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package userpkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author 小罗
*/
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name = request.getParameter("txtname");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
ServletContext ctx = getServletContext();
ctx.setAttribute("TxtName", name);
ctx.setAttribute("Email", email);
ctx.setAttribute("Phone", phone);
response.sendRedirect("Rewelcome.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
7、Rewelcome.jsp
<%--
Document : Rewelcome
Created on : 2024-4-19, 20:40:56
Author : 小罗
--%>
<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<%
ServletContext ctx = getServletContext();
String Username = (String)ctx.getAttribute("TxtName");
out.println("<h1>Welcome " + Username + " !</h1>");
%>
</head>
<body>
<a href="Download.jsp">Down load</a>
</body>
</html>
最后运行出来:
8、Download.jsp
<%--
Document : Download
Created on : 2024-4-19, 21:27:33
Author : 小罗
--%>
<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
ServletContext ctx = getServletContext();
String Username = (String)ctx.getAttribute("TxtName");
String email = (String)ctx.getAttribute("Email");
String phone = (String)ctx.getAttribute("Phone");
response.setHeader("Content-Disposition", "attachment; filename=\"User_Info.txt\"");
response.setContentType("text/plain");
PrintWriter downloadOut = response.getWriter();
downloadOut.println("Username : " + Username);
downloadOut.println("Email : " + email);
downloadOut.println("Phone : " + phone);
out.close();
downloadOut.close();
%>
点击 Down load 会下载txt文档,里面有用户刚注册得信息
下载用户信息