实现登录成功自动跳转

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'message.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    <%	
    	String message = (String)application.getAttribute("message"); 
    	out.write(message);
    
    %>
  



package day6;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo2 extends HttpServlet {

	/**
	 * The doGet method of the servlet. 
* * 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 { //test1(response); //test2(response); test3(request, response); } private void test3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "恭喜你登录成功,本大爷将在3秒后跳转,如果没有跳转,点我一下"; this.getServletContext().setAttribute("message", data); //转发 this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response); } private void test2(HttpServletResponse response) throws IOException { response.setHeader("refresh", "3;url='/day6/index.jsp'"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("恭喜你登录成功,本大爷将在3秒后跳转,如果没有跳转,点我一下"); } private void test1(HttpServletResponse response) throws IOException { //刷新,每隔3秒 response.setHeader("refresh", "3"); String data = new Random().nextInt(100000)+""; response.getWriter().write(data); } /** * The doPost method of the servlet.
* * 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 { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } }


你可能感兴趣的:(WEB开发)