j2ee—框架(1):Servlet+JSP实现基本的登录功能(v1.0)
2024-09-03 23:29:18
主要分为四个部分:LoginController、web.xml、login.jsp和login_success.jsp(login_fail.jsp)。
- 第一部分 LoginController
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext servletContext = getServletContext();
RequestDispatcher requestDispatcher = null; String name = request.getParameter("name");
System.out.println(name);
String password = request.getParameter("password"); //if("ustc".equals(name)&&"oreo".equals(password))
if(name.equals(password))
{
System.out.println("123");
requestDispatcher = servletContext.getRequestDispatcher("/login_success.jsp");
}
else
{
requestDispatcher = servletContext.getRequestDispatcher("/login_fail.jsp");
} requestDispatcher.forward(request, response); //response.getWriter().append("Served at: ").append(request.getContextPath());
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
在这一部分,用LoginController实现了跳转不同页面的处理,如果账号和密码是正确的,则跳转到login_success.jsp的页面,否则跳转到login_fail.jsp的页面。
- 第二部分 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Controller</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>hello.LoginController</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/action/a1</url-pattern>
</servlet-mapping>
</web-app>在这里,先看<servlet-mapping>中的内容,如果url后半部分是/action/a1,则将该部分交给Controller的LoginController处理。
- 第三部分 login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="action/a1" method="post">
<label>账号:</label><input type="text" name="name"></br>
<label>密码:</label><input type="text" name="password"></br>
<input type="submit" value="登录">
</form>
</body>
</html>在这里,声明了action="action/a1",所以会被交给servlet处理,servlet才能用String类型的数据将name和password保存下来,然后用来作为判定条件,从而跳转到相应的页面。
- 第四部分 login_success.jsp和login_fail.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome!
</body>
</html>
login_success.jsp代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Failed!
</body>
</html>
login_fail.jsp代码
运行截图
该部分将逻辑处理都放在了LoginController中完成,这种方式并不好,之后的版本会对这个问题进行改进,而且这里对web.xml使用的配置的方法,相对于注解的方式,更加利于后期的管理,尤其是在大项目中会体现出更好的优势。
最新文章
- ios录音、音频播放功能
- [js开源组件开发]html5标签audio的样式更改
- 简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
- servlet 笔记
- 网络编程中常见地址结构与转换(IPv4/IPv6)
- lambda高级进阶--组合函数
- ADC0832的应用
- 关于操作HDFS的一个问题
- LoadRunner监控数据库服务
- [转载:Q1mi]Bootstrap和基于Bootstrap的登录验证示例
- 电脑小白和ta的小白电脑——JAVA开发环境
- 56.关于vue项目的seo问题
- .NET Dispose模式的实现
- Spring MVC测试框架详解——服务端测试
- Autofac应用总结
- 使用cocoa捕获dock栏中的“退出”事件,解决qt开发的应用程序退出异常的问题
- El中调用java静态方法
- CEPH 创建 RPM 安装包
- Python使用paramiko库远程安全连接SSH
- Guava包学习--EventBus
热门文章
- jquery实现表单验证简单实例
- 「PHP」策略模式
- for循环删除列表中元素遇到的漏删的问题(python)
- Batch Normalization 批量标准化
- BZOJ:2763-[JLOI2011]飞行路线(最短路分层图)
- 《图说VR入门》——googleVR入门
- Android stdio build.gradle buildscript 里面的repositories 和allprojects里面 repositories 的区别
- CC3200底板测试-烧写CC3200-LAUNCHXL
- 一个体验好的Windows 任务栏缩略图开发心得
- 对JSON的理解