踏入mvc的Controller层,实现servlet配置。
登录页面
1.参考课堂例子,客户端通过login.jsp发出登录请求,请求提交到loginServlet处理。如果用户名和密码相同则视为登录成功,跳转到loginSuccess.jsp页面,显示“欢迎你”+用户名;否则跳转到loginFail.jsp页面,显示“登录失败”,通过超链接返回login.jsp。
loginServlet
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取提交的用户名和密码
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
// 这里简单比较用户名和密码是否相同,实际项目中应该从数据库中验证
if (username != null && password != null && username.equals(password)) {
// 登录成功,重定向到登录成功页面
// response.sendRedirect("loginSuccess.jsp?username=" + username);
RequestDispatcher dispatcher = request.getRequestDispatcher("loginSuccess.jsp?" );
dispatcher.forward(request, response);
} else {
// 登录失败,重定向到登录失败页面
// response.sendRedirect("loginFail.jsp");
RequestDispatcher dispatcher = request.getRequestDispatcher("loginFail.jsp");
dispatcher.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// GET 请求时直接调用 doPost 方法处理
doGet(request, response);
}
}
login.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="loginServlet" 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>
<button type="submit">Login</button>
</form>
</body>
</html>
loginSuccess.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<title>Login Success</title>
</head>
<body>
<h2>Login Success</h2>
<p>欢迎你,<%= request.getParameter("username") %>!</p>
</body>
</html>
loginFail.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<title>Login Fail</title>
</head>
<body>
<h2>Login Fail</h2>
<p>登录失败,请<a href="login.jsp">返回重试</a>。</p>
</body>
</html>
Servlet中读取参数
2.参考提供的“在Servlet中读取参数”内容,使用ServletConfig检索Servlet的初始化参数,请补充填写以下①-⑥空,并运行ConfigDemoServlet程序,将运行结果截图。
【步骤1】编写ConfigDemoServlet程序,内容如下:
package Servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ConfigDemoServlet extends HttpServlet{
String servletName=null;
ServletConfig config=null;
String email=null;
String telephone=null;
public void init() {
config =__getServletConfig()_________;
servletName = config.getServletName();
email=_config.getInitParameter("email"); ___;
telephone=__config.getInitParameter("telephone") __;
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("Servlet名称:"+servletName+"<br>");
out.println("Email地址:"+email+"<br>");
out.println("电话:"+telephone);
out.println("</body></html>");
}
}
【步骤2】修改web.xml文件,在其中添加下面Servlet定义和初始化参数定义
<servlet>
<servlet-name> ConfigDemoServlet </servlet-name>
<servlet-class>Servlet.ConfigDemoServlet </servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>hacker@163.com</param-value>
</init-param>
<init-param>
<param-name>telephone</param-name>
<param-value>8899123</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ConfigDemoServlet </servlet-name>
<url-pattern> /configDemo </url-pattern>
</servlet-mapping>
【步骤3】在浏览器的地址栏中输入下面URL访问该Servlet并截图显示结果:
实验心得
学长学姐说,多逃,多抄,哪位愿赐予我设计与建模的宝藏。