学习目标:
-实验名称:Servlet应用—
目的:能用JSP+Servlet+JavaBean的MVC开发模式开发Web项目。
内容:
编写用户信息相关的Java Bean,用MVC三层开发模式完成登录、注册等功能。
要求:
◆实验课前着手实验
组长组织小组演示验收,互评打分,组长将最终成绩发给老师
◆本次写实验报告,上传学习通
实验课上小组互评打分标准:
. 90-100分:完成实验,运行效果好。
- 80-89分:基本完成实验或有瑕疵,运行效果一般。.70-79分:实验进行了一半以上。
- 60-69:实验进行了不到一半。
. 60分以下:还在学习消化,没有开始实验。
代码MVC分层:
界面:
学习时间:
提示:这里可以添加计划学习的时间
例如:
- 周一至周五晚上 7 点—晚上9点
- 周六上午 9 点-上午 11 点
- 周日下午 3 点-下午 6 点
学习产出:
UserService.java
package com.nciae.service;
public class UserService {
public boolean login(String username,String password) {
if(password!=null && username.equals(password)){
return true;
}else{
return false;
}
}
public boolean register(String username, String password) {
if(password!=null && username.equals(password)){
return true;
}else{
return false;
}
}
}
UserServlet.java
package com.nciae.control;
import com.nciae.service.UserService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ServletContext application=this.getServletContext();
PrintWriter out = response.getWriter();
String path = request.getContextPath();
String username = request.getParameter("username");
String password = request.getParameter("password");
request.getSession().setAttribute("username",username);
UserService us = new UserService();
boolean userValued = us.register(username,password);
if (userValued ==true){
out.print("进入成功! 即将进入主页面");
response.setHeader("refresh","2;url="+path+"/index.jsp");
}else {
out.print("账号密码不正确! 即将返回初始页面");
response.setHeader("refresh","2;url="+path+"/register.jsp");
}
}
}
UserInfo.java
package com.nciae.pojo;
public class UserInfo {
private String username;
private int age;
private String password;
public UserInfo(String username, int age, String password) {
this.username = username;
this.age = age;
this.password = password;
}
public UserInfo() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"username='" + username + '\'' +
", age=" + age +
", password='" + password + '\'' +
'}';
}
}
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>登录页面</h1>
<form action="<%=request.getContextPath()%>/userServlet" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<br><br>
<input type="submit" value="登录">
</form>
<br>
<p>没有账户? <a href="register.jsp">点击注册</a></p>
</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>注册页面</h1>
<form action="<%=request.getContextPath()%>/userServlet" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<br><br>
<input type="submit" value="注册" onclick="alert('注册成功')">
</form>
<br>
<p>已有账户? <a href="login.jsp">点击登录</a></p>
</body>
</html>
Index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<body>
<h2>欢迎来到主页面!</h2>
<%
request.setCharacterEncoding("utf-8");
String username = request.getSession().getAttribute("username").toString();
%>
<h3 style="color: red">您好,<%=username %></h3>
</body>
</html>
- 技术笔记 2 遍
- CSDN 技术博客 3 篇
- 习的 vlog 视频 1 个