response.sendRedirect("/a.jsp");
页面的路径是相对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中,如:
response.sendRedirect("URL");
跳转后浏览器地址栏变化。
这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。
2) forward方式
RequestDispatcher dispatcher =request.getRequestDispatcher("/a.jsp");
dispatcher .forward(request, response);
Servlet页面跳转的路径是相对路径。forward方式只能跳转到本web应用中的页面上。
跳转后浏览器地址栏不会变化。
使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute
实例:
第一种方法:
AccountBean.java文件
package com.ht.servlet;
public class AccountBean {
private String username = "";
private String password = "";
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
CheckAccount.java文件
package com.ht.servlet;
import java.io.IOException;
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 CheckAccount extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req,HttpServletResponse resp)
throwsServletException, IOException {
doGet(req,resp);
}
@Override
public voiddoGet(HttpServletRequest req, HttpServletResponseresp)
throwsServletException, IOException {
HttpSession session =req.getSession();
AccountBean account = newAccountBean();
String username =req.getParameter("username");
String pwd =req.getParameter("pwd");
account.setPassword(pwd);
account.setUsername(username);
if((username !=null)&&(username.trim().equals("jsp"))){
if((pwd !=null)&&(pwd.trim().equals("1"))){
System.out.println("success");
session.setAttribute("account",account);
Stringlogin_suc ="success.jsp";
resp.sendRedirect(login_suc);
return;
}
}
String login_fail ="fail.jsp";
resp.sendRedirect(login_fail);
return;
}
}
第二种方法:
package servlets;
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 LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException, IOException {
String user =request.getParameter("user");
RequestDispatcher rd =null;
if(user!=null&&user.equals("admin"))
{
request.getSession().setAttribute("login","true");
rd=request.getRequestDispatcher("admin.html");
rd.forward(request,response);
}
elseif(user!=null&&user.equals("common"))
{
request.getSession().setAttribute("login","true");
rd =request.getRequestDispatcher("common.html");
rd.forward(request,response);
}
else
response.sendRedirect("login.html");
}
}