今天一直被这个问题困扰着,就是一个JSP页面中有两个表单form1、form2,分别对应着Servlet1、Servlet2,其中form1是上传文件的,可以在Servlet1中获得上传文件的名称等信息,由于要在form2的Servlet2中用此文件名称,要实现Servlet1和Servlet2的变量共享,一直找方法都找不到,最后采用了session来实现变量共享,其实挺简单的,参照如下文章:
原文:http://bbs.51cto.com/thread-654846-1.html
JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
–共享变量
? setAttribute
? getAttribute
–变量的作用域
? ServletContext
? HttpSession
? HttpServletRequest
–实例
? 测试变量的作用域
######################################################
? 共享变量
–无论对象的作用域如何,共享变量和获得变量的
方法都是一致的
? 共享变量
–setAttribute(“varName”,obj);
? 获得变量
–getAttribute(“varName”);
? 变量的作用域
–在Servlet中有三个作用域
? ServletContext
–范围最大,应用程序级别的,整个应用程序都能访问
? HttpSession
–次之,会话级别的,在当前的浏览器中都能访问
? HttpServletRequest
–范围最小,请求级别,请求结束,变量的作用域也结束
? 实例
–测试变量的作用域
同一页面测试变量
ScopeServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(sc_value);
out.print(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
获取同一页面变量
两页面获取变量
ScopeServlet2.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet2 extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet2() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
HttpSession session = request.getSession();
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(sc_value);
out.println(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
测试第二个页面获取共享变量
打开第二个页面测试
由ScopeServlet跳转转发到ScopeServlet3页面
ScopeServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);
/*
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(sc_value);
out.print(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
**/
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
ScopeServlet3.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet3 extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet3() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
HttpSession session = request.getSession();
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <h1>");
out.println("ScopeServlet3");
out.println(" </h1>");
out.println(sc_value);
out.println(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
超链接跳转测试
ScopeServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
//request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);
/*String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");**/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <a href=/Servlet_Scope/servlet/ScopeServlet3>");
out.println(" ScopeServlet3");
out.println(" </a>");
/*out.print(sc_value);
out.print(session_value);
out.println(request_value);**/
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
? setAttribute
? getAttribute
–变量的作用域
? ServletContext
? HttpSession
? HttpServletRequest
–实例
? 测试变量的作用域
######################################################
? 共享变量
–无论对象的作用域如何,共享变量和获得变量的
方法都是一致的
? 共享变量
–setAttribute(“varName”,obj);
? 获得变量
–getAttribute(“varName”);
? 变量的作用域
–在Servlet中有三个作用域
? ServletContext
–范围最大,应用程序级别的,整个应用程序都能访问
? HttpSession
–次之,会话级别的,在当前的浏览器中都能访问
? HttpServletRequest
–范围最小,请求级别,请求结束,变量的作用域也结束
? 实例
–测试变量的作用域
同一页面测试变量
ScopeServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(sc_value);
out.print(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
获取同一页面变量
两页面获取变量
ScopeServlet2.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet2 extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet2() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
HttpSession session = request.getSession();
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(sc_value);
out.println(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
测试第二个页面获取共享变量
打开第二个页面测试
由ScopeServlet跳转转发到ScopeServlet3页面
ScopeServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);
/*
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(sc_value);
out.print(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
**/
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
ScopeServlet3.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet3 extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet3() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
HttpSession session = request.getSession();
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <h1>");
out.println("ScopeServlet3");
out.println(" </h1>");
out.println(sc_value);
out.println(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
超链接跳转测试
ScopeServlet.java
package com.michael.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
//request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);
/*String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");**/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <a href=/Servlet_Scope/servlet/ScopeServlet3>");
out.println(" ScopeServlet3");
out.println(" </a>");
/*out.print(sc_value);
out.print(session_value);
out.println(request_value);**/
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
本文介绍JavaWeb开发中Servlet的变量共享方法及不同作用域的使用技巧,包括ServletContext、HttpSession和HttpServletRequest的区别与应用场景。
761

被折叠的 条评论
为什么被折叠?



