整体目录结构如下:
编写Bean,和Servlet
package com.ht.servlet;
public class AccountBean {
private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
package com.ht.servlet;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
/**
* Servlet implementation class AccountBean
*/
@WebServlet("/CheckAccount")
public class CheckAccount extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CheckAccount() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession sessionzxl = request.getSession();
AccountBean account = new AccountBean();
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
account.setPassword(pwd);
account.setUsername(username);
System.out.println("username :"+ username + " password :" + pwd);
if((username != null)&&(username.trim().equals("jspp"))) {
System.out.println("username is right!");
if((pwd != null)&&(pwd.trim().equals("1"))) {
System.out.println("success");
sessionzxl.setAttribute("account", account);
String login_suc = "success.jsp";
response.sendRedirect(login_suc);
return;
}
}
System.out.println("fail!");
String login_fail = "fail.jsp";
response.sendRedirect(login_fail);
return;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
登录的jsp页面如下Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
<form action="login">
username:<input type="text" name="username"><br>
password:<input type="password" name="pwd"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
登录成功界面如下success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="com.ht.servlet.AccountBean"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
<%AccountBean account = (AccountBean)session.getAttribute("account");%>
username:<%= account.getUsername()%> <br>
password:<%= account.getPassword() %> <br>
basePath: <%=basePath%><br>
path:<%=path%><br>
</body>
</html>
登录失败的jsp页面如下:fail.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My JSP 'fail.jsp' starting page</title>
</head>
<body>
Login Failed! <br>
basePath: <%=basePath%><br>
path:<%=path%><br>
</body>
</html>
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>ServletTest</display-name>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CheckAccount</servlet-name>
<servlet-class>com.ht.servlet.CheckAccount</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckAccount</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
描述一下上面运行过程:
在浏览器输入:http://localhost:8080/ServletTest/ 会通过欢迎页面welcome-file-list找到登录页面Login.jsp, 界面显示如下:
在登录页面输入用户名和密码,点击登录,找到对应的action, 会去运行/login其对应的servlet, 找到doGet()方法,判断用户名和密码
如果用户名密码不是jspp和1,就会跳转到失败页面fail.jsp
如果用户名等于jspp和1,则跳转到成功页面success.jsp
上面就是一个最简单的JSP和servlet例子。在运行上面例子中,有一个概念session.
在checkAccount.java中,直接通过request获取session
HttpSession sessionzxl = request.getSession();
后面将定义的变量存储到session中:sessionzxl.setAttribute("account", account);
在jsp中怎么获取session?
在success.jsp中,有这么一行<%AccountBean account = (AccountBean)session.getAttribute("account");%>,那么session来至于哪儿?
查看资料后得知,session是jsp隐式对象。
JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明。JSP隐式对象也被称为预定义变量。
JSP所支持的九大隐式对象:
对象 | 描述 |
---|---|
request | HttpServletRequest 接口的实例 |
response | HttpServletResponse 接口的实例 |
out | JspWriter类的实例,用于把结果输出至网页上 |
session | HttpSession类的实例 |
application | ServletContext类的实例,与应用上下文有关 |
config | ServletConfig类的实例 |
pageContext | PageContext类的实例,提供对JSP页面所有对象以及命名空间的访问 |
page | 类似于Java类中的this关键字 |
Exception | Exception类的对象,代表发生错误的JSP页面中对应的异常对象 |
session是jsp的内置对象,所以你可以直接写在jsp的
<%
session.setAttribute("a", b); //把b放到session里,命名为a,
String M = session.getAttribute(“a”).toString(); //从session里把a拿出来,并赋值给M
%>