Example analysis of the usage of response object in Java

  • 2020-04-01 04:34:10
  • OfStack

This article illustrates the use of the response object in Java. Share with you for your reference, as follows:

< The JSP: forward> The action element is used at run time to terminate the execution of the current page on the server side and to move from the current page to the specified page.

Use the setHeader() method of the response object to set the automatic refresh interval for the page. The statement to reload this page every 60 seconds is:

response.setHeader("refresh",60);

After 3 seconds, the browser loads the new page //www.jb51.net as follows:
response.setHeader("refresh","3;URL=//www.jb51.net");

Response method: void sendRedirect(String url), redirects the page to the specified url address.

Example: the user login function is implemented using response

Login.html is the login form page
Login.jsp is an information processing page used to verify that the user logged in successfully.
Success.jsp is the jump page after successful login.

Login.html source code is as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title> Login function instance </title>
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="this is my page">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
 </head>
 <body>
  <center>
  <h1> Login screen </h1>
  <form action="login.jsp" method="post">
         Name: <input type="text" name="name"><br>
     Password: <input type="password" name="pwd"><br>
    <input type="submit" name="submit" value=" The login ">
    <input type="reset" name="reset" value=" reset ">
  </form>
  </center>
 </body>
</html>

Login.jsp source code is as follows:


<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  <title> Login function instance </title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 </head>
 <body>
  <center>
  <h1> Login function instance </h1>
  <%
   request.setCharacterEncoding("UTF-8");
   String name = request.getParameter("name");
   String pwd = request.getParameter("pwd");
   if(name != null && pwd != null && name.equals("guanlin") && pwd.equals("123"))
   {
    //response.sendRedirect("success.jsp");
  %>
  <jsp:forward page="success.jsp"></jsp:forward>
  <%}else
  {
  out.println("<font color='red'> Wrong username or password, 5 Return to login page in seconds, click if you don't want to wait <a href='response/login.html'> Return to the login </a></font>");
  response.setHeader("refresh","5;url = login.html");
  }
  %>
  </center>
 </body>
</html>

The source code for success.jsp is as follows:


<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  <title> Login function instance </title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">  
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 </head>
 <body>
  <center>
  <h1 style="green"> Login successful !</h1>
  <%
   request.setCharacterEncoding("UTF-8");
   String name = request.getParameter("name");
   String pwd = request.getParameter("pwd");
   %>
    Login user name: <%=name %><br>
    The login password is: <%=pwd %>
  </center>
 </body>
</html>

I hope this article has been helpful to you in Java programming.


Related articles: