当前位置: 技术问答>java相关
servlet当中的页面转换
来源: 互联网 发布时间:2015-05-14
本文导语: 请问在servlet 当中怎么进行请求页面转换。比如我在servlet中经过判断后,决定转换到main.jsp页面。很急,请各位大侠多帮忙! | import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import ja...
请问在servlet 当中怎么进行请求页面转换。比如我在servlet中经过判断后,决定转换到main.jsp页面。很急,请各位大侠多帮忙!
|
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=Shift_JIS";
public void init() throws ServletException
{
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try {
ServletContext cont=this.getServletContext();
RequestDispatcher rd=cont.getRequestDispatcher("/main.jsp");
rd.forward(request,response);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doGet(request,response);
}
public void destroy()
{
}
}
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=Shift_JIS";
public void init() throws ServletException
{
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try {
ServletContext cont=this.getServletContext();
RequestDispatcher rd=cont.getRequestDispatcher("/main.jsp");
rd.forward(request,response);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doGet(request,response);
}
public void destroy()
{
}
}
|
除了 forward方式,还可以使用sendRedirect("/main.jsp")方式
两种方式区别:
前种是直接链接到新页面
后种,是Servlet向客户端发新页面的URL,由浏览器向新页面请求。
两种方式区别:
前种是直接链接到新页面
后种,是Servlet向客户端发新页面的URL,由浏览器向新页面请求。
|
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class check extends HttpServlet
{
public void init() {
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
response.sendRedirect("http://10.201.1.120:8080/examples/jsp/date.jsp");
}
}
肯定好使
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class check extends HttpServlet
{
public void init() {
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
response.sendRedirect("http://10.201.1.120:8080/examples/jsp/date.jsp");
}
}
肯定好使
|
forward
sendRedirect
sendRedirect