ServletContext与ServletConfig
ServletContext
- 整个JavaWeb工程可以用一个对象表示,这个对象就是ServletContext
ServletConfig
- 我们在web.xml中给某一个Servler配置一些配置信息,当服务器被启动的时候,这些配置信息就会被封装到某一个ServletConfig对象中去,因此ServletConfig表示的某一个Servlrt的配置文件
首先在eclipse中创建一个默认的Servlet文件
这个表示的就是Servlet对外访问的虚拟路径
package com.kilig.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Demo1Servelt")
public class Demo1Servelt extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取代表某一个JavaWeb工程的ServletContext对象
// Context对象是一个域对象,可以往某一个域对象中存放数据,并且还可以取出存放的数据
ServletContext context =this.getServletContext();
// 在context中存放数据
context.setAttribute("username", "张三");
// 取出数据
Object result =context.getAttribute("username");
// 打印输出结果
System.out.println(result);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
此外,在同一个工程目录下的其他Servlet的文件也可以获取Context中定义的数据
package com.kilig.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Demo2Servlet")
public class Demo2Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取ServletConfig对象
ServletConfig sc =this.getServletConfig();
// 获取sc对象中的参数信息
String encoding =sc.getInitParameter("encoding");
System.out.println(encoding);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
转发与重定向
- 实现转发调用的是HttpServletRequest对象中的方法,实现重定向调用的是HttpServletResponse对象中的方法
- 转发时浏览器的url地址栏不会发生改变,重定向时浏览器中的url地址会发生改变
- 转发时浏览器只请求一次服务器,重定向时浏览器请求两次服务器
首先创建一个index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="" method="post">
<p>UserName:<input type="text" name="username"></p>
<p>Password:<input type="password" name="password"></p>
<p>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</p>
</form>
</center>
</body>
</html>
成功界面success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success Page</title>
</head>
<body>
<h3 style="color:red">Success Page</h3>
</body>
</html>
失败界面fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Fail Page</title>
</head>
<body>
<h3 style="color:green">Fail Page</h3>
</body>
</html>
然后在JavaResous中新建一个LoginServlet类
package com.kilig.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;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取表单提交的数据
// getParameter()方法可以获取请求的参数信息
String usernameString= request.getParameter("username");
String passwordString= request.getParameter("password");
System.out.println("username is: "+usernameString);
System.out.println("password is: "+passwordString);
// 如果username=admin password=123 ,则跳转success.jsp否则跳转fail.jsp
// 1.通过转发实现跳转
if("admin".equals(usernameString) && "123".equals(passwordString))
request.getRequestDispatcher("/success.jsp").forward(request, response);
else
request.getRequestDispatcher("/fail.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
然后在配置一下web.xml。 编写路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>HelloWeb</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>
<!--给LoginServlet配置一个对外访问的虚拟路径 -->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.kilig.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
转发
重定向(使用重定向需要完整的路径)