servlet 下
11. 和属性相关的方法:
1). 方法
void setAttribute(String name, Object o): 设置属性
Object getAttribute(String name): 获取指定的属性
Enumeration getAttributeNames(): 获取所有的属性的名字组成的 Enumeration 对象
removeAttribute(String name): 移除指定的属性
2). pageContext, request, session, application 对象都有这些方法!
这四个对象也称之为域对象.
pageContext: 属性的作用范围仅限于当前 JSP 页面
request: 属性的作用范围仅限于同一个请求.
session: 属性的作用范围限于一次会话: 浏览器打开直到关闭称之为一次会话(在此期间会话不失效)
application: 属性的作用范围限于当前 WEB 应用. 是范围最大的属性作用范围, 只要在一处设置属性, 在其他各处的 JSP 或 Servlet 中
都可以获取到.
12. JSP:
1). WHY:
JSP 是简 Servlet 编写的一种技术, 它将 Java 代码和 HTML 语句混合在同一个文件中编写,
只对网页中的要动态产生的内容采用 Java 代码来编写,而对固定不变的静态内容采用普通静态 HTML 页面的方式编写。
2). Java Server Page: Java 服务器端网页. 在 HTML 页面中编写 Java 代码的页面.
3). helloworld:
新建一个 JSP 页面, 在 body 节点内的 <% %> 即可编写 Java 代码.
<body>
<%
Date date = new Date();
System.out.print(date);
%>
</body>
4). JSP 可以放置在 WEB 应用程序中的除了 WEB-INF 及其子目录外的其他任何目录中,
JSP 页面的访问路径与普通 HTML 页面的访问路径形式也完全一样。
5). JSP的运行原理: JSP 本质上是一个 Servlet.
每个JSP 页面在第一次被访问时, JSP 引擎将它翻译成一个 Servlet 源程序, 接着再把这个 Servlet 源程序编译成 Servlet 的 class 类文件.
然后再由WEB容器(Servlet引擎)像调用普通Servlet程序一样的方式来装载和解释执行这个由JSP页面翻译成的Servlet程序。
6). JSP 页面的隐含变量: 没有声明就可以使用的对象. JSP页面一共有 9 个隐含对象.
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
//使用 <% %> 编写的代码在此位置. 可以用到 request, response, pageContext, session
//application, config, out, page 这 8 个隐含对象. (实际上还可以使用一个叫 exception 的隐含对象
}
①. request: HttpServletRequest 的一个对象. *
②. response: HttpServletResponse 的一个对象(在 JSP 页面中几乎不会调用 response 的任何方法.)
③. pageContext: 页面的上下文, 是 PageContext 的一个对象. 可以从该对象中获取到其他 8 个隐含对象. 也可以从中获取到当前
页面的其他信息. (学习自定义标签时使用它) *
④. session: 代表浏览器和服务器的一次会话, 是 HttpSession 的一个对象. 后面详细学习. *
⑤. application: 代表当前 WEB 应用. 是 ServletContext 对象. *
⑥. config: 当前 JSP 对应的 Servlet 的 ServletConfig 对象(几乎不使用). 若需要访问当前 JSP 配置的初始化参数,
需要通过映射的地址才可以.
映射 JSP:
<servlet>
<servlet-name>hellojsp</servlet-name>
<jsp-file>/hello.jsp</jsp-file>
<init-param>
<param-name>test</param-name>
<param-value>testValue</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hellojsp</servlet-name>
<url-pattern>/hellojsp</url-pattern>
</servlet-mapping>
⑦. out: JspWriter 对象. 调用 out.println() 可以直接把字符串打印到浏览器上. *
⑧. page: 指向当前 JSP 对应的 Servlet 对象的引用, 但为 Object 类型, 只能调用 Object 类的方法(几乎不使用)
⑨. exception: 在声明了 page 指令的 isErrorPage="true" 时, 才可以使用. *
<%@ page isErrorPage="true" %>
pageContext, request, session, application(对属性的作用域的范围从小到大)
out, response, config, page 、 exception
6). JSP模版元素: JSP页面中的静态HTML内容称
7). JSP表达式(expression)提供了将一个 java 变量或表达式的计算结果输出到客户端的简化方式,
它将要输出的变量或表达式直接封装在<%= 和 %>之中。
<%
Date date = new Date();
out.print(date);
%>
<%= date %>
8). JSP脚本片断(scriptlet)是指嵌套在<% 和 %>之中的一条或多条Java程序代码。
多个脚本片断中的代码可以相互访问
<%
String ageStr = request.getParameter("age");
Integer age = Integer.parseInt(ageStr);
if(age >= 18){
%>
成人...
<%
}else{
%>
未成人...
<%
}
%>
9). JSP 声明: JSP 声明将 Java 代码封装在<%!和 %>之中,它里面的代码将被插入进 Servle t的 _jspService 方法的外面
(在 JSP 页面中几乎从不这样使用)
10). JSP注释的格式:<%-- JSP 注释 --%> <!-- HTML 注释 -->
区别: JSP 注释可以阻止 Java 代码的执行.
13 请求的转发和重定向:
1). 本质 区别: 请求的转发只发出了一次请求, 而重定向则发出了两次请求.
具体:
①. 请求的转发: 地址栏是初次发出请求的地址.
请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址
②. 请求转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象.
请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.
③. 请求的转发: 只能转发给当前 WEB 应用的的资源
请求的重定向: 可以重定向到任何资源.
④. 请求的转发: / 代表的是当前 WEB 应用的根目录
请求的重定向: / 代表的是当前 WEB 站点的根目录.
14. HttpServlet:
1). 是一个 Servlet, 继承自 GenericServlet. 针对于 HTTP 协议所定制.
2). 在 service() 方法中直接把 ServletReuqest 和 ServletResponse 转为 HttpServletRequest 和 HttpServletResponse.
并调用了重载的 service(HttpServletRequest, HttpServletResponse)
在 service(HttpServletRequest, HttpServletResponse) 获取了请求方式: request.getMethod(). 根据请求方式有创建了
doXxx() 方法(xxx 为具体的请求方式, 比如 doGet, doPost)
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException("non-HTTP request or response");
}
service(request, response);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1. 获取请求方式.
String method = request.getMethod();
//2. 根据请求方式再调用对应的处理方法
if("GET".equalsIgnoreCase(method)){
doGet(request, response);
}else if("POST".equalsIgnoreCase(method)){
doPost(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
// TODO Auto-generated method stub
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
3). 实际开发中, 直接继承 HttpServlet, 并根据请求方式复写 doXxx() 方法即可.
4). 好处: 直接由针对性的覆盖 doXxx() 方法; 直接使用 HttpServletRequest 和 HttpServletResponse, 不再需要强转.
15. GenericServlet:
1). 是一个 Serlvet. 是 Servlet 接口和 ServletConfig 接口的实现类. 但是一个抽象类. 其中的 service 方法为抽象方法
2). 如果新建的 Servlet 程序直接继承 GenericSerlvet 会使开发更简洁.
3). 具体实现:
①. 在 GenericServlet 中声明了一个 SerlvetConfig 类型的成员变量, 在 init(ServletConfig) 方法中对其进行了初始化
②. 利用 servletConfig 成员变量的方法实现了 ServletConfig 接口的方法
③. 还定义了一个 init() 方法, 在 init(SerlvetConfig) 方法中对其进行调用, 子类可以直接覆盖 init() 在其中实现对 Servlet 的初始化.
④. 不建议直接覆盖 init(ServletConfig), 因为如果忘记编写 super.init(config); 而还是用了 SerlvetConfig 接口的方法,
则会出现空指针异常.
⑤. 新建的 init(){} 并非 Serlvet 的生命周期方法. 而 init(ServletConfig) 是生命周期相关的方法.
public abstract class GenericServlet implements Servlet, ServletConfig {
/** 以下方法为 Servlet 接口的方法 **/
@Override
public void destroy() {}
@Override
public ServletConfig getServletConfig() {
return servletConfig;
}
@Override
public String getServletInfo() {
return null;
}
private ServletConfig servletConfig;
@Override
public void init(ServletConfig arg0) throws ServletException {
this.servletConfig = arg0;
init();
}
public void init() throws ServletException{}
@Override
public abstract void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException;
/** 以下方法为 ServletConfig 接口的方法 **/
@Override
public String getInitParameter(String arg0) {
return servletConfig.getInitParameter(arg0);
}
@Override
public Enumeration getInitParameterNames() {
return servletConfig.getInitParameterNames();
}
@Override
public ServletContext getServletContext() {
return servletConfig.getServletContext();
}
@Override
public String getServletName() {
return servletConfig.getServletName();
}
}
16.代码区
package com.atguigu.javaweb; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ForwardServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("ForwardServlet‘s doGet"); request.setAttribute("name", "abcde"); System.out.println("ForwardServlet‘s name: " + request.getAttribute("name")); //请求的转发. //1. 调用 HttpServletRequest 的 getRequestDispatcher() 方法获取 RequestDispatcher 对象 //调用 getRequestDispatcher() 需要传入要转发的地址 String path = "testServlet"; RequestDispatcher requestDispatcher = request.getRequestDispatcher("/" + path); //2. 调用 HttpServletRequest 的 forward(request, response) 进行请求的转发. requestDispatcher.forward(request, response); } }
package com.atguigu.javaweb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class LoginServlet extends GenericServlet { //对当前的 Servlet 进行初始化: 覆盖 init 方法 @Override public void init(javax.servlet.ServletConfig config) throws ServletException { super.init(config); } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //获取请求方式是 GET 还是 POST HttpServletRequest httpServletRequest = (HttpServletRequest) request; String method = httpServletRequest.getMethod(); System.out.println(method); //1. 获取请求参数: username, password String username = request.getParameter("username"); String password = request.getParameter("password"); //2. 获取当前 WEB 应用的初始化参数: user, password. //需要使用 ServletContext 对象. String initUser = getServletContext().getInitParameter("user"); String initPassword = getServletContext().getInitParameter("password"); PrintWriter out = response.getWriter(); //3. 比对 //4. 打印响应字符串. if(initUser.equals(username) && initPassword.equals(password)){ out.print("Hello: " + username); }else{ out.print("Sorry: " + username); } } }
package com.atguigu.javaweb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet2 extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { // 获取请求方式是 GET 还是 POST String method = request.getMethod(); System.out.println(method); // 1. 获取请求参数: username, password String username = request.getParameter("username"); String password = request.getParameter("password"); // 2. 获取当前 WEB 应用的初始化参数: user, password. // 需要使用 ServletContext 对象. String initUser = getServletContext().getInitParameter("user"); String initPassword = getServletContext().getInitParameter("password"); PrintWriter out = response.getWriter(); // 3. 比对 // 4. 打印响应字符串. if (initUser.equals(username) && initPassword.equals(password)) { out.print("Hello: " + username); } else { out.print("Sorry: " + username); } } }
package com.atguigu.javaweb; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet3 extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; PrintWriter out = resp.getWriter(); try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql:///atguigu"; String user = "root"; String password2 = "1230"; connection = DriverManager.getConnection(url, user, password2); String sql = "SELECT count(id) FROM users WHERE username = ? " + "AND password = ?"; statement = connection.prepareStatement(sql); statement.setString(1, username); statement.setString(2, password); resultSet = statement.executeQuery(); StringBuilder result = new StringBuilder(); result.append("<html>") .append(" <head>") .append(" </head>") .append("</html>"); if(resultSet.next()){ int count = resultSet.getInt(1); if(count > 0){ out.print(result.toString()); }else{ out.print("Sorry: " + username); } } } catch (Exception e) { e.printStackTrace(); } finally{ try { if(resultSet != null){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(statement != null){ statement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(connection != null){ connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
package com.atguigu.javaweb; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 自定义的一个 Servlet 接口的实现类: 让开发的任何 Servlet 都继承该类. 以简化开发 */ public abstract class MyGenericServlet implements Servlet, ServletConfig { /** 以下方法为 Servlet 接口的方法 **/ @Override public void destroy() {} @Override public ServletConfig getServletConfig() { return servletConfig; } @Override public String getServletInfo() { return null; } private ServletConfig servletConfig; @Override public void init(ServletConfig arg0) throws ServletException { this.servletConfig = arg0; init(); } public void init() throws ServletException{} @Override public abstract void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException; /** 以下方法为 ServletConfig 接口的方法 **/ @Override public String getInitParameter(String arg0) { return servletConfig.getInitParameter(arg0); } @Override public Enumeration getInitParameterNames() { return servletConfig.getInitParameterNames(); } @Override public ServletContext getServletContext() { return servletConfig.getServletContext(); } @Override public String getServletName() { return servletConfig.getServletName(); } }
package com.atguigu.javaweb; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 针对于 HTTP 协议定义的一个 Servlet 基类 */ public class MyHttpServlet extends MyGenericServlet { @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { if(arg0 instanceof HttpServletRequest){ HttpServletRequest request = (HttpServletRequest)arg0; if(arg1 instanceof HttpServletResponse){ HttpServletResponse response = (HttpServletResponse)arg1; service(request, response); } } } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 获取请求方式. String method = request.getMethod(); //2. 根据请求方式再调用对应的处理方法 if("GET".equalsIgnoreCase(method)){ doGet(request, response); }else if("POST".equalsIgnoreCase(method)){ doPost(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // TODO Auto-generated method stub } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
package com.atguigu.javaweb; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RedirectServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("RedirectServlet‘s doGet"); request.setAttribute("name", "xyzmn"); System.out.println("RedirectServlet‘s name: " + request.getAttribute("name")); //执行请求的重定向, 直接调用 response.sendRedirect(path) 方法, //path 为要重定向的地址 String path = "testServlet"; response.sendRedirect(path); } }
package com.atguigu.javaweb; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestAttr */ public class TestAttr extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); //1. 在 Servlet 中无法得到 pageContext 对象 //2. request Object requestAttr = request.getAttribute("requestAttr"); out.print("requestAttr: " + requestAttr); out.print("<br><br>"); //3. session Object sessionAttr = request.getSession().getAttribute("sessionAttr"); out.print("sessionAttr: " + sessionAttr); out.print("<br><br>"); //4. application Object applicationAttr = getServletContext().getAttribute("applicationAttr"); out.print("applicationAttr: " + applicationAttr); out.print("<br><br>"); } }
package com.atguigu.javaweb; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TestServlet‘s doGet 方法. "); System.out.println("TestServlet‘s name: " + request.getAttribute("name")); } }
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>user</param-name> <param-value>atguigu</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>123567</param-value> </context-param> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>com.atguigu.javaweb.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/loginServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>ForwardServlet</display-name> <servlet-name>ForwardServlet</servlet-name> <servlet-class>com.atguigu.javaweb.ForwardServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ForwardServlet</servlet-name> <url-pattern>/forwardServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>com.atguigu.javaweb.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/testServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>RedirectServlet</display-name> <servlet-name>RedirectServlet</servlet-name> <servlet-class>com.atguigu.javaweb.RedirectServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RedirectServlet</servlet-name> <url-pattern>/redirectServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>hellojsp</servlet-name> <jsp-file>/hello.jsp</jsp-file> <init-param> <param-name>test</param-name> <param-value>testValue</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hellojsp</servlet-name> <url-pattern>/hellojsp</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>TestAttr</display-name> <servlet-name>TestAttr</servlet-name> <servlet-class>com.atguigu.javaweb.TestAttr</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestAttr</servlet-name> <url-pattern>/testAttr</url-pattern> </servlet-mapping> </web-app>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>AAA PAGE</h4>
<a href="b.jsp">TO B PAGE</a>
</body>
</html>
<%@page import="java.util.Date"%> <%@ 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=UTF-8"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("pageContextAttr", "pageContextValue"); request.setAttribute("requestAttr", "requestValue"); session.setAttribute("sessionAttr", "sessionValue"); application.setAttribute("applicationAttr", "applicationValue"); %> <h2>Attr 111 Page: <%= new Date() %></h2> <br><br> pageContextAttr: <%= pageContext.getAttribute("pageContextAttr") %> <br><br> requestAttr: <%= request.getAttribute("requestAttr") %> <br><br> sessionAttr: <%= session.getAttribute("sessionAttr") %> <br><br> applicationAttr: <%= application.getAttribute("applicationAttr") %> <br><br> <a href="attr_2.jsp">To Attr2 Page</a> <br><br> <a href="testAttr">To Attr Servlet</a> <% request.getRequestDispatcher("/attr_2.jsp").forward(request, response); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.Date"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Attr 222 Page: <%= new Date() %></h2> <br><br> pageContextAttr: <%= pageContext.getAttribute("pageContextAttr") %> <br><br> requestAttr: <%= request.getAttribute("requestAttr") %> <br><br> sessionAttr: <%= session.getAttribute("sessionAttr") %> <br><br> applicationAttr: <%= application.getAttribute("applicationAttr") %> </body> </html>
<%@ 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=UTF-8"> <title>Insert title here</title> </head> <body> <h4>BBB PAGE</h4> <% //1. 请求转发的代码: //request.getRequestDispatcher("/c.jsp").forward(request, response); //request.getRequestDispatcher("http://www.atguigu.com").forward(request, response); //2. 请求的重定向 response.sendRedirect(request.getContextPath()+"/c.jsp"); //response.sendRedirect("http://www.atguigu.com"); %> </body> </html>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>CCC PAGE</h4>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page isErrorPage="true" %>
<!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>
</body>
</html>
<%@page import="java.util.Date"%> <%@ 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> <% Date date = new Date(); //System.out.print("-->" + date); %> <% String str = date.toString(); //没有声明就可以使用的对象 String name = request.getParameter("name"); System.out.println(name); // System.out.println(response instanceof HttpServletResponse); ServletRequest req = pageContext.getRequest(); System.out.print(req == request); System.out.println(session.getId()); System.out.println(application.getInitParameter("password")); System.out.println(config.getInitParameter("test")); out.println(request.getParameter("name")); out.println("<br><br>"); out.println(application.getInitParameter("password")); out.println("<br><br>"); out.println(this); out.println("<br><br>"); out.println(page); /*null true true50F1C99C85D6821D43A1CA2DC23787E4 123567 null */ %> </body> </html>
<%@page import="java.util.Date"%> <%@ 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=UTF-8"> <title>Insert title here</title> </head> <body> <% Date date = new Date(); out.print(date); %> <br><br> <!-- JSP 表达式 --> <%= date %> <% String ageStr = request.getParameter("age"); Integer age = Integer.parseInt(ageStr); if(age >= 18){ %> 成人... <% }else{ %> 未成人... <% } %> <%! void test(){} %> <%-- JSP 注释 --%><!-- HTML 注释 --> <!-- <% System.out.println("打印信息..."); %> --> </body> </html>
<!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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginServlet" method="post"> user: <input type="text" name="username"/> password: <input type="password" name="password"/> <input type="submit" value="Submit"/> </form> </body> </html>
<!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=UTF-8"> <title>Insert title here</title> </head> <body> <a href="loginServlet">Test</a> <br><br> <a href="forwardServlet">Forward</a> <br><br> <a href="redirectServlet">Redirect</a> </body> </html>