Part V _servlet in-depth analysis of continuation and re-discussion JavaBean

Source: Internet
Author: User

Practice continued:

Answer: A, B, D, E, F, G

where c, there is no Request.setparameter () method.

Request forwarding and redirection

public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexception{ Request.setattribute ("Test", "hello")//Request forward Request.getrequestdispatcher ("/test4.jsp"). Forward (request, response) ;//redirect//Response.sendredirect ("test4.jsp");} <%--test4.jsp--%><body><%= request.getattribute ("test")%></body>

The way to request forwarding, from the perspective of the browser access to the address bar Localhost:8080/test/helloservlet, the page appears hello, the address bar information remains unchanged, and redirect the way the Address bar access localhost:8080/test/ After HelloServlet, the page jumps to: localhost:8080/test/test4.jsp, the page information is null. Essentially, the underlying analysis, the way the request is forwarded from the servlet to the JSP is always the same request object, and the redirect is that once the sendredirect is called (noting that the response object is called), the response arrives at the client first, Tells the client to request the page test.jsp again, so the client sends a request again, and the former request object is already ecstasy, replaced by a new request object, and the result is null.

Examples of the scope of JavaBean

package Com.test.servlet;public class Student{private long classNo;private String name; private int Age;private Boolean sexy;public Student ()//constructor {Classno = 970431;name = "Zzzl"; age = 34;sexy = true;} /** * @return return to age. */public int Getage () {return age;} /** * @param age to set. */public void setage (int age) {this.age = age;} /** * @return return to Classno. */public long Getclassno () {return classno;} /** * @param classno * To set the Classno. */public void Setclassno (Long classno) {This.classno = Classno;} /** * @return return name. */public String GetName () {return name;} /** * @param name * To set the name. */public void SetName (String name) {this.name = name;} /** * @return return to sexy. */public boolean issexy ()//The Get method that returns a Boolean type can also be substituted for {return sexy;} /** * @param sexy * To set the sexy. */public void Setsexy (Boolean sexy) {this.sexy = sexy;}} 
<%@ page language= "java" pageencoding= "gb2312"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

Visit the javabean.jsp page, you can see that with the Refresh JSP page, the time is also constantly refreshed, but if the scope of the date is changed to application, as long as the server does not stop, this time will never change, and change it to session, as long as the current session is not finished, the time will not change , that is, the Refresh page time has not changed, but the re-opening of a page will change. As the request comes in, the time changes as the refresh (the client requests the action).

Part V _servlet in-depth analysis of continuation and re-discussion JavaBean

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.