- 浏览: 166012 次
- 性别:
- 来自: 北京
最新评论
-
lian819:
赞一个, 对理解hash有很大的帮助
HashSet中hashCode的作用 -
albrich:
有warn对实际没什么影响对吧?
Could not find property [struts.valueStack] -
edmond207:
绝对好使,非常感谢!
修改oracle 10g xe 的字符集 -
javaanddonet:
请问:
楼主在服务层中注入的时候,采用的是构造方法注入还是Se ...
Spring 循环依赖(circular reference)问题 -
txlaijava:
感谢分享咯. ~
oracle插入特殊字符
Servlet中的几个对象:
HttpServletRequest、HttpSession、ServletContext,这三个对象都有
void setAttribute(String name, Object o)和void removeAttribute(String name)
这两个方法,通过这两个方法我们可以很方便的进行参数的传递。
三个对象的作用域的大小分别由小到大。当我们把一个对象通过void setAttribute(String name, Object o)添加到这三个对象中的时候,除非我们显示的调用void removeAttribute(String name)或者当这三个对象自己销毁否则不会释放这些对象占用的空间,所以我们在将一个对象通过setAttribute添加到这三个对象时,需要注意用完了就remove掉(当然用完以后这个添加到的对象本身就快销毁时,我们也没必要多此一举)。而且尽量当对象存放在HttpServletRequest,然后才是HttpSession最后才是ServletContext。尽量不要把大对象存放在这些对象中,因为一个进程占用的资源毕竟是有限的。
Servlet中路径跳转的问题:
让我们先通过一个例子来看一下HttpServletRequest的获取路径的一些方法的返回值。
ServletPath:
package squall.servlet.path;
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;
public class ServletPath extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
showParam(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
showParam(req,resp);
}
private void showParam(HttpServletRequest req, HttpServletResponse resp) throws IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
out.println(docType + "<HTML> " + "<HEAD><TITLE> pathshow"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+"getContextPath(): " + req.getContextPath() + " </br>"
+"getPathInfo(): " + req.getPathInfo() + " </br>"
+"getRequestURI(): " + req.getRequestURI()+ " </br>"
+"getRequestURL(): " + req.getRequestURL()+ " </br>"
+"getServletPath(): " + req.getServletPath()+ " </br>"
+"getQueryString(): " + req.getQueryString()+ " </br>"
);
out.println("</body></html>");
}
}
将此Servlet部署后,mapping至<url-pattern>/ServletPath/*</url-pattern>
在浏览器输入http://127.0.0.1:8080/train/ServletPath/a/b/c?name=1&passord=2
可以看到如下输出:
getContextPath(): /train
getPathInfo(): /a/b/c
getRequestURI(): /train/ServletPath/a/b/c
getRequestURL(): http://127.0.0.1:8080/train/ServletPath/a/b/c
getServletPath(): /ServletPath
getQueryString(): name=1&passord=2
我们可以很清楚的看到各个方法的返回值的情况。
一个html,Servlet,jsp访问另外一个Servlet时候可以通过相对路径(相对于当前的路径),相对于同一Web应用下的路径(比如我们的/train,通过request.getContextPath()获得)和绝对路径(直接http://...)。
我们一般在Servlet中访问另外一个Servlet或者jsp可以通过Servlet输出中的HTML元素FORM的属性值ACTION来实现。但是我们有时候可以采用一些更加方便的方法。
下面简单的介绍3个方法:
RequestDispatcher的
void forward(ServletRequest request, ServletResponse response)和
void include(ServletRequest request, ServletResponse response)方法。
和
HttpResponse的void sendRedirect(String location)方法。
我们先来介绍HttpResponse的void sendRedirect(String location)方法。
这个方法将请求一个url为location的页面, location可以是一个相对路径、相对于当前WEB应用的路径或者直接Http开头的路径,下面让我们看一个例子吧。
BeforeSendRedirect:
package squall.servlet.path;
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;
public class BeforeSendRedirect extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
req.setAttribute("canyousee", "this");
out.println(docType + "<HTML> " + "<HEAD><TITLE> BeforeSendRedirect"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+" <h1>BeforeSendRedirect</h1> </br>"
);
out.println("</body></html>");
resp.sendRedirect("AfterSendRedirect");
System.err.println("canyousee " + req.getAttribute("canyousee") + " in BeforeSendRedirect");
System.err.println("end in BeforeSendRedirect");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req,resp);
}
}
AfterSendRedirect:
package squall.servlet.path;
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;
public class AfterSendRedirect extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
System.err.println("begin AfterSendRedirect!");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
out.println(docType + "<HTML> " + "<HEAD><TITLE> AfterSendRedirect"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>AfterSendRedirect</h1> </br>");
out.println("</body></html>");
System.err.println("canyousee " + req.getAttribute("canyousee") + " in AfterSendRedirect");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
将BeforeSendRedirect mapping至<url-pattern>/BeforeSendRedirect</url-pattern>
将BeforeSendRedirect mapping至<url-pattern>/AfterSendRedirect</url-pattern>
然后我们访问http://127.0.0.1:8080/train/BeforeSendRedirect ,可以看到页面跳转到http://127.0.0.1:8080/train/AfterSendRedirect,并且页面上显示的内容也是
AfterSendRedirect并且tomcat控制台输出为:
canyousee this in BeforeSendRedirect
end in BeforeSendRedirect
begin AfterSendRedirect
canyousee null in AfterSendRedirect
可以看出使用HttpResponse的void sendRedirect(String location)方法
1、 html页面会显示location所输出的页面
2、 浏览器上的url也为location的绝对路径
3、 原servlet调用void sendRedirect(String location)方法后,剩下的代码也会执行
4、 原servlet和跳转至的servlet的HttpServletRequest对象不是同一个对象。
下面该是
RequestDispatcher的
void forward(ServletRequest request, ServletResponse response)和
void include(ServletRequest request, ServletResponse response)方法的介绍。
在这之前,我们先来看下javax.servlet.RequestDispatcher对象的获得方法。
获得这个对象的方法,存在于3个类或者接口中分别是ServletContext、ServletRequest和ServletRequestWrapper。这里我们只看ServletContext和ServletRequest。
在ServletContext和ServletRequest中获取RequestDispatcher都是通过方法RequestDispatcher getRequestDispatcher(String path),但是需要注意这个方法在用不同的类调用的时候存在一些差异,使用ServletContext接口的对象调用时,String path的参数必须以”/”开头,也就是说从这个web项目的根开始。而使用ServletRequest接口的对象调用这个方法时,path可以不以”/”开头,简单的说就是可以使用相对于当前请求的相对路径。
下面我们先来看RequestDispatcher的void forward(ServletRequest request, ServletResponse response)方法,按照惯例,我们还是用例子来说明问题。
BeforeForward:
package squall.servlet.path;
import java.io.IOException;
import java.io.PrintWriter;
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 BeforeForward extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
req.setAttribute("canyousee", "this");
out.println(docType + "<HTML> " + "<HEAD><TITLE> BeforeForward"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>BeforeForward</h1> </br>");
out.println("</body></html>");
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/AfterForward");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in BeforeForward");
rd.forward(req, resp);
System.err.println("end in BeforeForward");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
AfterForward:
package squall.servlet.path;
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;
public class AfterForward extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
System.err.println("begin AfterForward!");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
out.println(docType + "<HTML> " + "<HEAD><TITLE> AfterForward"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>AfterForward</h1> </br>");
out.println("</body></html>");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in AfterForward");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
BeforeForward和AfterForward分别mapping至<url-pattern>/BeforeForward</url-pattern>和<url-pattern>/AfterForward</url-pattern>
可以看到控制台输出如下:
canyousee this in BeforeForward
begin AfterForward!
canyousee this in AfterForward
end in BeforeForward
浏览器上显示的为:
AfterForward浏览器上url为http://127.0.0.1:8080/train/BeforeForward
从结果我们可以看到
1、 forward会显示forward后的页面的内容
2、 forward调用后浏览器的url不会变化
3、 forward后的页面和原页面的request为同一对象
下面是RequestDispatcher的void include(ServletRequest request, ServletResponse response)例子了,这个例子里,我们使用request来获取RequestDispatcher对象,所以可以使用相对路径。
BeforeInclude:
package squall.servlet.path;
import java.io.IOException;
import java.io.PrintWriter;
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 BeforeInclude extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
req.setAttribute("canyousee", "this");
out.println(docType + "<HTML> " + "<HEAD><TITLE> BeforeInclude"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>BeforeInclude</h1> </br>");
RequestDispatcher rd = req.getRequestDispatcher(
"InInclude");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in BeforeInclude");
rd.include(req, resp);
out.println(" <h1>AfterInclude</h1> </br>");
out.println("</body></html>");
System.err.println("end in BeforeInclude");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
InInclude:
package squall.servlet.path;
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;
public class InInclude extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
System.err.println("In InInclude!");
PrintWriter out = resp.getWriter();
out.println( " <h1>In InClude</h1> </br>");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in InInclude");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
BeforeInclude和InInclude分别mapping至<url-pattern>/path/BeforeInclude</url-pattern>和<url-pattern>/path/InInclude</url-pattern>
访问http://127.0.0.1:8080/train/path/BeforeInclude
可以看到浏览器显示为:
BeforeInclude
In InClude
AfterInclude
而控制台输出为:
canyousee this in BeforeInclude
In InInclude!
canyousee this in InInclude
end in BeforeInclude
需要注意的是上面的例子中的InInclude无法输出一个完整的html页面。
从结果可以看出:
1、 include会显示原页面中include的地方显示include的Servlet输出的内容
2、 forward调用后浏览器的url不会变化
3、 forward后的页面和原页面的request为同一对象
还是要强调一下InInclude无法输出一个完整的html页面,它只是输出一部分东西而已。
讲到这里基本上servlet中请求的转发和跳转已经讲完了。
HttpServletRequest、HttpSession、ServletContext,这三个对象都有
void setAttribute(String name, Object o)和void removeAttribute(String name)
这两个方法,通过这两个方法我们可以很方便的进行参数的传递。
三个对象的作用域的大小分别由小到大。当我们把一个对象通过void setAttribute(String name, Object o)添加到这三个对象中的时候,除非我们显示的调用void removeAttribute(String name)或者当这三个对象自己销毁否则不会释放这些对象占用的空间,所以我们在将一个对象通过setAttribute添加到这三个对象时,需要注意用完了就remove掉(当然用完以后这个添加到的对象本身就快销毁时,我们也没必要多此一举)。而且尽量当对象存放在HttpServletRequest,然后才是HttpSession最后才是ServletContext。尽量不要把大对象存放在这些对象中,因为一个进程占用的资源毕竟是有限的。
Servlet中路径跳转的问题:
让我们先通过一个例子来看一下HttpServletRequest的获取路径的一些方法的返回值。
ServletPath:
package squall.servlet.path;
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;
public class ServletPath extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
showParam(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
showParam(req,resp);
}
private void showParam(HttpServletRequest req, HttpServletResponse resp) throws IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
out.println(docType + "<HTML> " + "<HEAD><TITLE> pathshow"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+"getContextPath(): " + req.getContextPath() + " </br>"
+"getPathInfo(): " + req.getPathInfo() + " </br>"
+"getRequestURI(): " + req.getRequestURI()+ " </br>"
+"getRequestURL(): " + req.getRequestURL()+ " </br>"
+"getServletPath(): " + req.getServletPath()+ " </br>"
+"getQueryString(): " + req.getQueryString()+ " </br>"
);
out.println("</body></html>");
}
}
将此Servlet部署后,mapping至<url-pattern>/ServletPath/*</url-pattern>
在浏览器输入http://127.0.0.1:8080/train/ServletPath/a/b/c?name=1&passord=2
可以看到如下输出:
getContextPath(): /train
getPathInfo(): /a/b/c
getRequestURI(): /train/ServletPath/a/b/c
getRequestURL(): http://127.0.0.1:8080/train/ServletPath/a/b/c
getServletPath(): /ServletPath
getQueryString(): name=1&passord=2
我们可以很清楚的看到各个方法的返回值的情况。
一个html,Servlet,jsp访问另外一个Servlet时候可以通过相对路径(相对于当前的路径),相对于同一Web应用下的路径(比如我们的/train,通过request.getContextPath()获得)和绝对路径(直接http://...)。
我们一般在Servlet中访问另外一个Servlet或者jsp可以通过Servlet输出中的HTML元素FORM的属性值ACTION来实现。但是我们有时候可以采用一些更加方便的方法。
下面简单的介绍3个方法:
RequestDispatcher的
void forward(ServletRequest request, ServletResponse response)和
void include(ServletRequest request, ServletResponse response)方法。
和
HttpResponse的void sendRedirect(String location)方法。
我们先来介绍HttpResponse的void sendRedirect(String location)方法。
这个方法将请求一个url为location的页面, location可以是一个相对路径、相对于当前WEB应用的路径或者直接Http开头的路径,下面让我们看一个例子吧。
BeforeSendRedirect:
package squall.servlet.path;
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;
public class BeforeSendRedirect extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
req.setAttribute("canyousee", "this");
out.println(docType + "<HTML> " + "<HEAD><TITLE> BeforeSendRedirect"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+" <h1>BeforeSendRedirect</h1> </br>"
);
out.println("</body></html>");
resp.sendRedirect("AfterSendRedirect");
System.err.println("canyousee " + req.getAttribute("canyousee") + " in BeforeSendRedirect");
System.err.println("end in BeforeSendRedirect");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req,resp);
}
}
AfterSendRedirect:
package squall.servlet.path;
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;
public class AfterSendRedirect extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
System.err.println("begin AfterSendRedirect!");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
out.println(docType + "<HTML> " + "<HEAD><TITLE> AfterSendRedirect"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>AfterSendRedirect</h1> </br>");
out.println("</body></html>");
System.err.println("canyousee " + req.getAttribute("canyousee") + " in AfterSendRedirect");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
将BeforeSendRedirect mapping至<url-pattern>/BeforeSendRedirect</url-pattern>
将BeforeSendRedirect mapping至<url-pattern>/AfterSendRedirect</url-pattern>
然后我们访问http://127.0.0.1:8080/train/BeforeSendRedirect ,可以看到页面跳转到http://127.0.0.1:8080/train/AfterSendRedirect,并且页面上显示的内容也是
AfterSendRedirect并且tomcat控制台输出为:
canyousee this in BeforeSendRedirect
end in BeforeSendRedirect
begin AfterSendRedirect
canyousee null in AfterSendRedirect
可以看出使用HttpResponse的void sendRedirect(String location)方法
1、 html页面会显示location所输出的页面
2、 浏览器上的url也为location的绝对路径
3、 原servlet调用void sendRedirect(String location)方法后,剩下的代码也会执行
4、 原servlet和跳转至的servlet的HttpServletRequest对象不是同一个对象。
下面该是
RequestDispatcher的
void forward(ServletRequest request, ServletResponse response)和
void include(ServletRequest request, ServletResponse response)方法的介绍。
在这之前,我们先来看下javax.servlet.RequestDispatcher对象的获得方法。
获得这个对象的方法,存在于3个类或者接口中分别是ServletContext、ServletRequest和ServletRequestWrapper。这里我们只看ServletContext和ServletRequest。
在ServletContext和ServletRequest中获取RequestDispatcher都是通过方法RequestDispatcher getRequestDispatcher(String path),但是需要注意这个方法在用不同的类调用的时候存在一些差异,使用ServletContext接口的对象调用时,String path的参数必须以”/”开头,也就是说从这个web项目的根开始。而使用ServletRequest接口的对象调用这个方法时,path可以不以”/”开头,简单的说就是可以使用相对于当前请求的相对路径。
下面我们先来看RequestDispatcher的void forward(ServletRequest request, ServletResponse response)方法,按照惯例,我们还是用例子来说明问题。
BeforeForward:
package squall.servlet.path;
import java.io.IOException;
import java.io.PrintWriter;
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 BeforeForward extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
req.setAttribute("canyousee", "this");
out.println(docType + "<HTML> " + "<HEAD><TITLE> BeforeForward"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>BeforeForward</h1> </br>");
out.println("</body></html>");
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/AfterForward");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in BeforeForward");
rd.forward(req, resp);
System.err.println("end in BeforeForward");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
AfterForward:
package squall.servlet.path;
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;
public class AfterForward extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
System.err.println("begin AfterForward!");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
out.println(docType + "<HTML> " + "<HEAD><TITLE> AfterForward"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>AfterForward</h1> </br>");
out.println("</body></html>");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in AfterForward");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
BeforeForward和AfterForward分别mapping至<url-pattern>/BeforeForward</url-pattern>和<url-pattern>/AfterForward</url-pattern>
可以看到控制台输出如下:
canyousee this in BeforeForward
begin AfterForward!
canyousee this in AfterForward
end in BeforeForward
浏览器上显示的为:
AfterForward浏览器上url为http://127.0.0.1:8080/train/BeforeForward
从结果我们可以看到
1、 forward会显示forward后的页面的内容
2、 forward调用后浏览器的url不会变化
3、 forward后的页面和原页面的request为同一对象
下面是RequestDispatcher的void include(ServletRequest request, ServletResponse response)例子了,这个例子里,我们使用request来获取RequestDispatcher对象,所以可以使用相对路径。
BeforeInclude:
package squall.servlet.path;
import java.io.IOException;
import java.io.PrintWriter;
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 BeforeInclude extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
req.setAttribute("canyousee", "this");
out.println(docType + "<HTML> " + "<HEAD><TITLE> BeforeInclude"
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
+ " <h1>BeforeInclude</h1> </br>");
RequestDispatcher rd = req.getRequestDispatcher(
"InInclude");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in BeforeInclude");
rd.include(req, resp);
out.println(" <h1>AfterInclude</h1> </br>");
out.println("</body></html>");
System.err.println("end in BeforeInclude");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
InInclude:
package squall.servlet.path;
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;
public class InInclude extends HttpServlet
...{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
System.err.println("In InInclude!");
PrintWriter out = resp.getWriter();
out.println( " <h1>In InClude</h1> </br>");
System.err.println("canyousee " + req.getAttribute("canyousee")
+ " in InInclude");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
...{
doGet(req, resp);
}
}
BeforeInclude和InInclude分别mapping至<url-pattern>/path/BeforeInclude</url-pattern>和<url-pattern>/path/InInclude</url-pattern>
访问http://127.0.0.1:8080/train/path/BeforeInclude
可以看到浏览器显示为:
BeforeInclude
In InClude
AfterInclude
而控制台输出为:
canyousee this in BeforeInclude
In InInclude!
canyousee this in InInclude
end in BeforeInclude
需要注意的是上面的例子中的InInclude无法输出一个完整的html页面。
从结果可以看出:
1、 include会显示原页面中include的地方显示include的Servlet输出的内容
2、 forward调用后浏览器的url不会变化
3、 forward后的页面和原页面的request为同一对象
还是要强调一下InInclude无法输出一个完整的html页面,它只是输出一部分东西而已。
讲到这里基本上servlet中请求的转发和跳转已经讲完了。
发表评论
-
Servlet介绍(5)-----listener
2009-04-08 16:42 1146一、listener 在Web项目中,我们可以 ... -
Servlet介绍(4)-----filter
2009-04-08 16:38 1109filter的意思简单的说就是过滤器,它的作用就是当用户请求一 ... -
Servlet介绍(3)-----cookie和session
2009-04-08 16:30 1515一、关于cookie和session 1、cookie ... -
Servlet介绍(2)-----读取表单参数
2009-04-08 16:28 6498一、HttpServlet读取html中f ... -
Servlet介绍(1)
2009-04-08 16:23 1155Servlet的简单介绍 文章多转自http://ww ...
相关推荐
Servlet API提供了`HttpSession`接口来管理用户会话。会话跟踪允许在多个请求之间保持状态,例如购物车功能。源码中,可以研究`HttpSession`的创建、失效逻辑,以及如何添加和获取会话属性。 六、安全性增强 ...
javax.servlet.http.HttpSession javax.servlet.RequestDispatcher javax.servlet.SingleThreadModel javax.servlet.ServletInputStream javax.servlet.ServletContextEvent javax.servlet.ServletOutputStream ...
Servlet API提供了`HttpSession`接口,用于跟踪用户会话。通过会话,服务器可以在多个请求之间保持状态,例如存储用户的购物车信息。 10. **MVC模式** 虽然Servlet API本身并不直接支持Model-View-Controller...
8. **会话管理**:Servlet可以使用`HttpSession`接口来跟踪用户会话,存储和检索用户特定的数据,例如购物车信息或用户登录状态。 9. **过滤器(Filter)**:Servlet Filter允许在请求到达Servlet之前对其进行拦截...
Servlet 2.0引入了HttpSession接口,允许开发者存储和检索与用户会话相关的数据。这意味着当用户在网站上浏览多个页面时,可以保持他们的状态,如购物车信息或登录状态。 3. **过滤器(Filter)**: 过滤器是...
6. **过滤器Filter**:Servlet-api还提供了过滤器机制,允许在请求到达Servlet之前或之后进行预处理和后处理。通过实现`javax.servlet.Filter`接口,可以创建过滤器来拦截、修改或增强请求和响应。 7. **监听器...
6. **会话管理**:Servlet 2.4支持会话跟踪,通过`HttpSession`接口实现。会话可以用来存储用户状态信息,通过`getSession()`方法获取,`setAttribute()`和`getAttribute()`用于设置和获取属性。 7. **多线程模型**...
- **HttpServletRequest接口**: - `getParameter(String name)`: 获取指定名称的参数值。 - `getParameterValues(String name)`: 获取指定名称的所有参数值数组。 - `setAttribute(String name, Object value)`:...
这些内容在servlet3.txt、servlet2.txt和servelt1.txt文件中应该有详细介绍,包括具体方法的使用示例、配置示例以及最佳实践。通过学习这些文档,开发者可以更好地理解和使用Servlet API,从而高效地开发Java Web...
- **Session管理**:提供了` HttpSession`接口,用于在客户端和服务器之间维护状态。 - **Request和Response对象**:`HttpServletRequest`和`HttpServletResponse`提供了处理HTTP请求和响应所需的方法和属性。 这两...
getAttribute(String) - Method in interface javax.servlet.http.HttpSession Returns the object bound with the specified name in this session, or null if no object is bound under the name. ...
6. **Servlet配置**: 通过`<servlet>`元素定义Servlet,包括Servlet的类名、初始化参数等。`<servlet-config>`元素用于设置Servlet的特定配置信息。 7. **请求处理**: `HttpServletRequest`接口提供了获取请求参数...
6. **EL (Expression Language) 和 JSTL (JavaServer Pages Standard Tag Library)**: - 支持 EL 表达式语言和 JSTL 标签库,简化了在 JSP 页面中的数据绑定和逻辑处理。 7. **资源引用**: - 增强了资源引用的...
javax.servlet.http.HttpSession.class javax.servlet.http.HttpSessionContext.class javax.servlet.http.Cookie.class javax.servlet.http.HttpSessionAttributeListener.class javax.servlet....
10. **会话管理**:Servlet可以通过`HttpSession`接口来管理用户的会话状态,如设置和获取会话属性、检测会话是否过期等。 在`javax`包中,除了Servlet相关的类,可能还包含了其他Javax库的类,这些类可能涉及Java ...
Servlet通过`HttpServletRequest`和`HttpServletResponse`对象来获取请求信息和构建响应。例如,获取请求参数: ```java String param = request.getParameter("paramName"); ``` 并设置响应: ```java ...
6. **Servlet API**:`HttpServletRequest`和`HttpServletResponse`接口,它们提供了处理HTTP请求和构建响应的方法。 7. **Servlet的部署**:创建`Servlet`类,实现`Servlet`接口或继承`HttpServlet`,然后在`web....
- Servlet API提供了`HttpSession`接口,用于管理客户端与服务器之间的会话状态。 - 可以通过`getSession()`方法获取或创建一个新的会话,`setAttribute()`和`getAttribute()`用来存储和检索会话数据。 7. **过滤...
6. **Servlet 3.0及以上版本** 自Servlet 3.0开始,Servlet API引入了一些新的特性,比如注解驱动的Servlet和Filter注册,异步处理能力等。如果需要这些新特性,需要确保使用的`javax.servlet.jar`对应的是Servlet ...
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; ``` 这里导入了实现过滤器所需的包,包括处理HTTP请求和响应的基本类。 ...