"Introduction to Javaweb Program development" after class

Source: Internet
Author: User
Tags session id

Chapter I.

1, please write a well-formed XML document, the requirements include a football team, the team named Madrid, 5 players: Ronaldo, Casillas, Ramos, Modric, Benzema, basketball team, team named Lakers, 2 players: Oneal, Bryant. to contain comments.

2. In the XML Schema document, define an employee's age as 18~58周岁. Please write out the corresponding element declaration.

------answer to question 1th------

<?xml version= "1.0" encoding= "gb2312" standalone= "yes"?>< team list ><!--soccer team Madrid-->< Team type = "football team" >< Team name >madrid</>< team name >< name >ronaldo</name >< name >casillas</name >< name > ramos</name >< name >modric</name >< name >benzema</name ></Team ></Team ><!--basketball team Lakers- ->< Team Type = "basketball team" >< team name >lakers</team name >< player >< name >oneal</name >< name >bryant</name ></Players ></Team ></Team list >

------answer to question 2nd------

<xs:element name= "Age" ><xs:simpleType>  <xs:restriction base= "Xs:integer" >    <xs: MinInclusive value= "/>    <xs:maxinclusive value="/> </xs:restriction></xs  : Simpletype></xs:element>
Chapter II

1. How do I publish a web app to a tomcat host on a localhost, and write at least 3 implementations?

2. What configuration can be made in the Web. xml file of the CHAPTER02 application to configure the Welcome.html page as the default page for the app?

------answer to question 1th------

1. Deploy the Web application directly to Tomcat/webapps

2. Configure the Web App through Tomcat/conf/server.xml

3. Create an XML file and configure the Web app information to place the XML file Tomcat\conf\catalina\localhost

------answer to question 2nd------

The following configuration can be done in the Web-app tab:

<welcome-file-list>        <welcome-file>welcome.html</welcome-file></welcome-file-list>
Chapter III

1, briefly describe the communication process of HTTP1.1 protocol?

2. What is the difference between a POST request and a GET request?

------answer to question 1th------

1, the client and the server side to establish a TCP connection.

2. The client can send multiple requests to the server, and when the next request is sent, there is no need to wait for the result of the last request.

3. The server must return the response results in the order in which the client requests are accepted.

4. The client makes a request to close the TCP connection

5. Server-side shutdown TCP connection

------answer to question 2nd------

1, post transmission data size Unlimited

2. Post is more secure than GET request mode

Fourth Chapter

1, write a servlet, to achieve statistical site access to the function of the number of times.

2, please write a program, so that the program can read the configuration information of the servlet, from which the parameter named encoding corresponding parameters value, and output to the page.

------answer to question 1th------

The steps are as follows:

1) Create a new class Showtimesservlet inherit HttpServlet, overriding the Doget and Dopost methods

2) Call the Doget method in the Dopost method, in the Doget method to achieve the function of the number of visits to the site, the user every request servlet, so that the number of visits times plus 1

3) Get ServletContext to remember the number of times after the last visit through its functions

The specific code is implemented as follows:

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;p ublic class Showtimesservlet extends HttpServlet {private static final long serialversionuid = 1l;protected void Doget (HttpServletRequest req, HttpServletResponse resp) th Rows Servletexception, IOException {ServletContext context = Getservletcontext (); Integer times = (integer) Context.getattribute ("Times"); if (times = = null) {times = new integer (1);} else {times = new Integer (Times.intvalue () + 1) ;}        Set the encoding, or the Chinese will become garbled resp.setcontenttype ("text/html;charset=gb2312");               Gets the output stream object PrintWriter out=resp.getwriter ();        Out.println ("

------answer to question 2nd------

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;p ublic class Myservlet extends HttpServlet { protected void doget (HttpServletRequest request,httpservletresponse response) throws Servletexception, IOException { PrintWriter out = Response.getwriter (); ServletConfig config = Getservletconfig (); String param = config.getinitparameter ("encoding"); Out.println ("encoding=" + param);} protected void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception, IOException { This.doget (request, Response);}}
Fifth Chapter

Please design a class that implements the download resource anti-theft chain according to the following requirements.

1) Create a Downmanagerservlet class that inherits the HttpServlet class.

2) in the Doget () method, determine whether the resource can be downloaded

------answer------

The steps are as follows:

1) Create a Downmanagerservlet class that inherits the HttpServlet class and overrides the Doget () method of the class.

2) in the Doget () method, use the GetHeader ("Referer") method of the HttpServletRequest object to get the value of the field referer and determine whether the resource can be downloaded.

3) If the download is not available, go to the download page.

The specific code is implemented as follows:

public class Downmanagerservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {response.setcontenttype ("text/html;charset=utf-8");        PrintWriter out = Response.getwriter ();        Gets the value of the Referer header string referer = Request.getheader ("Referer"); Gets the access address string Sitepart = "http://" + request.getservername (); if (referer! = null && referer.startswith (sitepart ) {//Handle the request being downloaded out.println ("Dealing download ..."); else {//illegal download request jumps to download.html page RequestDispatcher rd = request.getrequestdispatcher ("/download.html"); Rd.forward (    request, response);} }}
Sixth chapter

1. Use cookie technology to display the user's last access time function.

2, please design a class, using session technology to achieve the function of shopping cart.

------answer to question 1th------

The steps are as follows:

1) Create a Lastaccessservlet class that inherits the HttpServlet class and overrides the Doget () method of the class.

2) in the Doget () method, use Request.getcookies () to get an array of cookies formed by all cookies and traverse them.

3) If the Lastaccess attribute is found in the cookie information during traversal, the output is otherwise created, the cookie object is set, and the value is given to the client at the current time.

4) The cookie survives for 1 hours, and all resource clients accessing the current app echo the cookie information.

The specific code is implemented as follows:

Package Cn.itcast.chapter06.cookie.example;import Java.io.ioexception;import Java.util.date;import Javax.servlet.servletexception;import Javax.servlet.http.cookie;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class Lastaccessservlet extends HttpServlet {    private static final long Serialversionuid = 1L; & nbsp;  public void doget (httpservletrequest request,    httpservletresponse response ) throws Servletexception, IOException {response.setcontenttype ("Text/html;charset=utf-8");         /*     * Set a cookie's name:lastaccesstime          * read client sends cookie to get user last access time display */         string LastAccessTime = null;        //Get all the cookies and store them in the array          cookie[] cookies = request.getcookies ();         for (int i = 0; cookies = null && i < cookies.length; i++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N Bsp;    if ("Lastaccess". Equals (Cookies[i].getname ())) {                 //if the name of the cookie is lastaccess, obtain the value of the cookie                  lastaccesstime = Cookies[i]. GetValue ();                 break;            }         }        //to determine if there is a cookie   named lastaccess       if (LastAccessTime = = null) {            response.getwriter (). Print ("You are the first to visit this site!!!" ");         } else {             response.getwriter (). Print ("Your last Access Time" +lastaccesstime);         }        //Create a cookie to send the current time as the value of a cookie to the client          cookie cookie = new Cookie ("Lastaccess", New Date (). toLocaleString ());         cookie.setmaxage (60*60);//save 1 hours          //Cookie        cookie.setpath When accessing chapter06 resources ("/ Chapter06 ");         //send cookie         response.addcookie (cookie);     }}

------answer to question 2nd------

The steps are as follows:

1) Create a Purchaseservlet class that inherits the HttpServlet class and overrides the Doget () method of the class.

2) in the Doget () method, use Request.getsession () to implement the shopping cart.

3) Save the session ID in the cookie.

4) Redirect to the shopping cart page.

The specific code is implemented as follows:

public class Purchaseservlet extends HttpServlet {    public void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {        // Get the product         string id = req.getparameter ("id") purchased by the user;         if (id = = NULL) {             //if ID is null, redirect to Listbookservlet page              string url = "/chapter06/listbookservlet";             resp.sendredirect (URL);             Return;        }        book Book = Bookdb.getbook (ID);         //create or get the user's session object     &nbsP;    httpsession session = Req.getsession ();         Get the user's cart         List<Book> cart from the Session object = (List) session.getat Tribute ("cart");         if (cart = = null) {             //first Purchase, create a shopping cart for users (List collection mock shopping cart)              cart = new arraylist<book> ();             //the shopping city to the session object              session.setattribute ("cart", cart);        }         //the item into the shopping cart         cart.add (book);         //Create a cookie to hold the session's identification number &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;  cookie cookie = new Cookie ("Jsessionid", Session.getid ());         cookie.setmaxage,         cookie.setpath ("/chapter06 ");         resp.addcookie (cookie);         //Redirect to Shopping cart page         string url = "/chapter06/cartservlet";         resp.sendredirect (URL);     }}
Seventh Chapter

1, please use the include tag to write two JSP pages,

Requirements: Output b.jsp page content, wait 5 seconds, and then output a.jsp page.

2. A datetime.jsp page is known to display the current time. Please write a JSP

The file is used to display the "Welcome to the Wisdom Podcast, now the time is:" + current time.

------answer to question 1th------

A.jsp Code:

<%@ page contenttype= "Text/html;charset=utf-8"%><%thread.sleep (%>a.jsp), Chinese <br>

B.jsp Code:

<%@ page contenttype= "Text/html;charset=utf-8"%>b.jsp in Chinese <br><jsp:include page= "a.jsp" flush= "true" />

------answer to question 2nd------

<% @page language= "java" contenttype= "text/html;        Charset=utf-8 "%>
Eighth Chapter

1. Write a class that implements the ability to get the full "package. Class" name through the object.

2. Design a program to assign a value to the Person object (JavaBean Class) using the Beanutils tool.

1) Generate the user object directly.

2) Use the Beanutils tool to assign a value of "Youjun" to the Name property, and an age value of 31.

3) Use the Beanutils tool to remove the attribute value and output it in the console.

------answer to question 1th------

Package Cn.itcast.javabean;class Cs{}public class Getclassnamedemo {public static void main (string[] args) {CS CS = new CS (); System.out.println (Cs. GetClass (). GetName ());}}

------answer to question 2nd------

Package Cn.itcast.chapter08.beanutils;import Org.apache.commons.beanutils.beanutils;import Cn.itcast.chapter08.javabean.person;public class BeanUtilsDemo01 {public static void main (string[] args) throws Except        ion{person p = new person ();        Use Beanutils to assign a value to a property Beanutils.setproperty (p, "name", "Youjun");        Beanutils.setproperty (P, "age", 31);        Use Beanutils to get the property value String name = Beanutils.getproperty (P, "name");        String age = Beanutils.getproperty (P, "age");    System.out.println ("The name is" +name+ ", this year" +age+ "years old"); }}
Nineth Chapter

1. What is the MVC design pattern?

2. Briefly describe the role of model module in MVC design mode.

------answer to question 1th------

The MVC pattern divides a software program into 3 core modules: Model, view, and controller

------answer to question 2nd------

1. Managing Business data for Applications

2. Define business rules for access control and modification of this data

3. Provide a way for the view to query the model state

"Introduction to Javaweb Program development" after class

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.