当前位置: 代码迷 >> Web前端 >> Servlets & JSP Series 二 - Web APP Architecture
  详细解决方案

Servlets & JSP Series 二 - Web APP Architecture

热度:804   发布时间:2013-11-23 10:52:51.0
Servlets & JSP Series 2 - Web APP Architecture

?

?

Servlets & JSP Series 2 - Web APP Architecture

  • Servlets do not have a main() method, they are under the control of another Java application called a Container. Tomcat is an example of a container, when web server application gets a request for a servlet the server hands the request not to the servlet itself, but to the container in which the servlet is deployed, it’s the container that gives the servlet the HTTP request and response, and it’s the container that calls the servlet’s methods.
  • Container get to concentrate more on it own business logic instead of worrying about writing code for threading, security, and networking.
  • How the container handles a request: 1.User clicks a link that has a URL to a servlet instead of a static page; 2.The container “sees” that the request is for a servlet, so the container creates two objects: HttpServletResponse & HttpServletRequest; 3.The container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request, and passes the request and response objects to the servlet thread; 4.The container calls the servlet’s service() method, depending on the type of request, the service() method calls either the doGet() or doPost() method; 5.The doGet() method generates the dynamic page and stuffs the page into the response object; 6.The thread completes, the container converts the response object into HTTP response, sends it back to the client, then deletes the request and response objects.
  • In the real world, 99 percent of all servlets? override either the doGet() or doPost() method, 99 percent of all servlets are HttpServlets, servlet inherited from HttpServlet.
  • A servlet can have three names: 1.Client-know URL name; 2.Deployer-known secret internal name; 3.Actual file name.
  • When you deploy your servlet into your web Container, you will create a fairly simple XML document called the Deployment Descriptor to tell the Container how to run your servlets and JSPs, although you will use the DD for more than just mapping names, you will use two XML elements to map URLs to servlets-one to map the client-known public URL name to your own internal name, and the other to map your own internal name to a fully-qualified class name.
  • ? The deployment descriptor(DD) provides a “declarative” mechanism for customizing your web applications without touching source code.
  • The servlet does whatever it needs to do to process the request(like insert or search the database) and returns the HTML page in the HTTP response, all of the business logic and the client HTML page response is inside the servlet code.
  • Client fills out the DQL query form and submit, this send and HTTP POST request for the DoDQLQuery, then the web server invokes the servlet, the servlet runs the query on the database, then the request is forwarded to the appropriate JSP, then the JSP builds the response HTML and sends it back.
  • Model*View*Controller(MVC) takes business logic out of the servlet, and puts it in a “Model”-a reusable plain old java class, the model is a combination of the business data and the methods that operate on that data.
  • MVC in the Servlet & JSP world: 1.MODEL holds the real business logic and the state, in other words, it knows the rules for getting and updating the state, it’s the only part of the system that talks to the database; 2.VIEW is responsible for the presentation, it gets the state of the model from the controller, it’s also the part that gets the user input that goes back to the controller; 3.CONTROLLER takes user input from the request and figures out what it means to the model and tells the model to update itself, and makes the new model state available for the view.
  • A J2EE application server includes both a web container and an EJB container; Tomcat is a web container but not a full J2EE application server; A J2EE 1.4 server includes the Servlet spec 2,3, JSP spec 2.0, and EJB spec 2.1.
  • In the old days, say, the year 2000, could fine complete J2EE application servers, standalone web containers, and standalone EJB containers, but today, virtually all EJB containers are part of full J2EE servers, although there are still a few standalone web containers, including Tomcat and Resin, standalone web containers are usually configured to work with an HTTP web server(like Apache), although the Tomcat container does have the ability to act as a basic HTTP server, but for HTTP server capability, tomcat is not nearly as robust as robust as Apache, so the most common no-EJB web apps usually use Apache and Tomcat configured together-with Apache as the HTTP web server, and Tomcat as the web container, some of the most common J2EE servers are BEA’s WebLogic, the open source JBoss AS, and IBM’s WebSphere.

?

  相关解决方案