web| detailed 2.2.4 Deploy HTML file
In the HelloApp directory, add the index.htm file, which is used only to display a string of linked characters "Welcome to HelloApp", which links to login.jsp files. The following is the code for the index.htm file:
The URL to access index.htm is http://localhost:8080/helloapp/index.htm, and the results of the page are shown in Figure 2-3.
Figure 2-3 Index.htm
2.2.5 Deployment JSP
Next, create two JSP files, one of which is login.jsp (see routine 2-1), which displays the login page, asks for a username and password, and links to a servlet called Dispatcherservlet. There is also a JSP file is hello.jsp (see routine 2-2), this JSP is called by Dispatcherservlet to display the Hello page. The syntax for JSP is discussed in detail in chapter 4th, and this section focuses on introducing the publishing process for JSP. All two JSP files should be placed in the HelloApp directory.
Routine 2-1 login.jsp
Routine 2-2 hello.jsp
A loginform form is generated in login.jsp, which has two fields: username and Passoword. The URL to access login.jsp is http://localhost:8080/helloapp/login.jsp, and the resulting page is shown in Figure 2-4.
Figure 2-4 login.jsp Web page
2.2.6 Deployment Servlet
Next, create a servlet file, Named Dispatcherservlet.java (see routine 2-3), which calls the GetParameter method of the HttpServletRequest object to read the LoginForm form data submitted by the customer, obtains the user name and password, and then saves the user name and password in the Httpser Vletrequest the properties of the object, and then the request is forwarded to the hello.jsp.
Routine 2-3 Dispatcherservlet.java
Package Mypack;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;p ublic class Dispatcherservlet extends httpservlet{private String target = "/hello.jsp"; public void init (ServletConfig config) throws servletexception {super.init (config); public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio n {//If It is a GET request forward to DoPost () doPost (request, response); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti On {//Get the username from the request String username = Request.getparameter ("username"); Get the password from the request String password = request.getparameter ("password"); Add the user to the request Request.setattribute ("user", username); Request.setattribute ("PASSWORD", PASSWORD); Forward the request to the target named ServletcontExt context = Getservletcontext (); System.out.println ("redirecting to" + target); RequestDispatcher dispatcher = Context.getrequestdispatcher (target); Dispatcher.forward (request, response); public void Destroy () {}}
|
The steps for compiling and publishing Dispatcherservlet are as follows.
(1) Compiling Dispatcherservlet.java. At compile time, you need to set the Java Servlet API jar file (Servlet-api.jar) to the Classpath,servlet-api.jar file located in the The/common/lib directory.
(2) Copy the compiled class file to the/helloapp/web_inf/classes directory. The storage location of the Dispatcherservlet.class is/helloapp/web_inf/classes/mypack/dispatcherservlet.
In this case, the declaration puts the Dispatcherservlet class under the package Mypack, so you should create the subdirectory/mypack under the/web_inf/classes directory, and then devolve the Dispatcherservlet.class file in the subdirectory.
(3) Next in Web.xml, add the Dispatcherservlet class And Elements.
<web-app><servlet> <servlet-name>dispatcher</servlet-name> <servlet-class >mypack. Dispatcherservlet</servlet-class></servlet><servlet-mapping> <servlet-name> Dispatcher</servlet-name> <url-pattern>/dispatcher</url-pattern></servlet-mapping> </web-app>
|
The attribute description of the element is shown in table 2-4.
In this case configuration, the Load-on-startup property is not set for Dispatcherservlet, so when the Web application starts, the servlet container does not load the servlet, which is only loaded when the Web client accesses the servlet for the first time.
Table 2-4 Attributes of an element
| Property |
Description |
| Servlet-name |
Define the name of the servlet |
| Servlet-class |
Specifies the class that implements this servlet |
| Init-param |
Defines the initialization parameters (including parameter names and parameter values) of the servlet, and one element can have multiple |
| Load-on-startup |
Specifies the order in which the servlet is loaded when the Web application is started. When this value is positive or 0, the servlet container loads the servlet with a small numeric value, and then loads the other large servlet in turn. If this value is negative or not set, the servlet container will load it when the Web client first accesses the servlet |
element is used to specify And Mapping. Refers to the relative URL path that accesses the servlet.
Based on the above property, the URL that accesses the Dispatcherservlet is http://localhost:8080/helloapp/dispatcher,DispatcherServlet after the client requests, The request is then forwarded to the hello.jsp,hello.jsp generated page as shown in Figure 2-5.
Figure 2-5 Dispatcherservlet invoke hello.jsp generated Web page
2.2.7 Deployment JSP Tag Library
Finally, the tag library is added to the Web application. The tag library provides users with the ability to customize JSP tags. We're going to define a tag library called Mytaglib, which contains a simple hello tag that allows all of the --> resolves to the string "Hello". The following are steps for creating and publishing a Mytaglib tag library.
(1) Write the class Hellotag.java for handling the Hello tag, and routine 2-4 lists the source code for the Hellotag.java.
Routine 2-4 Hellotag.java
Package Mypack;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.jsptagexception;import Javax.servlet.jsp.tagext.tagsupport;public class Hellotag extends tagsupport{public void Hellotag () {}//method Called when the closing Hello tag was encountered public int doendtag () throws Jspexception {try {// We use the PageContext to get a Writer//We then print the text string Hello pagecontext.getout (). Print ("Hello"); catch (Exception e) {throw new Jsptagexception (E.getmessage ()); //We want to return skip_body because this tag does not support//a tag body return skip_body; The public void release () {//Call the parent's release to release the "any"//used by the Paren T tag. This is just good practice for when you start creating//hierarchies of tags. Super.release (); }}
|
When compiling Hellotag.java, you need to add the Jsp-api.jar file to the Classpath, which is located in the The/common/lib directory. Compile-generated hellotag.class are stored in/web-inf/classes/mypack/hellotag.class locations.
(2) Create a description file Mytaglib.tld file for the tag library, in which you define the Mytaglib tag library and the Hello tag. This file is stored in a/web-inf/mytaglib.tld location. Routine 2-5 lists the source code for the MYTAGLIB.TLD.
Routine 2-5 Mytaglib.tld
<?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE taglib public "-//sun Microsystems, INC.//DTD JSP Tag Library 1.1//en " "http://java.sun.com/j2ee/ Dtds/web-jsptaglibrary_1_1.dtd "><!--a tag library descriptor--><taglib> <tlibversion>1.0 </tlibversion> <jspversion>1.1</jspversion> <shortname>mytaglib</shortname > <uri>/mytaglib</uri> <tag> <name>hello</name> < Tagclass>mypack. hellotag</tagclass> <bodycontent>empty</bodycontent> <info>just Says hello< /info> </tag></taglib>
|
(3) Add in Web.xml file element, routine 2-6 lists the modified Web.xml file.
Routine 2-6 Join Elements of the Web.xml
<?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE web-app public '-//sun Microsystems, Inc. DTD Web application 2.3//en ' http://java.sun.com/j2ee /dtds/web-app_2_3.dtd ' ><web-app> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>mypack. Dispatcherservlet </servlet-class> </servlet> <servlet-mapping> < servlet-name>dispatcher</servlet-name> <url-pattern>/dispatcher</url-pattern> </servlet-mapping> <taglib> <taglib-uri>/mytaglib</taglib-uri> < Taglib-location>/web-inf/mytaglib.tld </taglib-location> </taglib></web-app>
|
Contains two properties And 。 which Specifies the tag library designator; Specifies the location of the tag library's description file (TLD).
(4) Add the Hello tag to the hello.jsp file. First, add the taglib directive that references mytaglib in hello.jsp:
<%@ taglib uri= "/mytaglib" prefix= "MM"%>
|
In the above taglib directive, prefix is used to specify the prefix for referencing the mytaglib tag library, and the modified hello.jsp file see routine 2-7.
Routine 2-7 add tag tag to hello.jsp
<%@ taglib uri= "/mytaglib" prefix= "MM"%> |
Hello.jsp the changes, and then access index.htm→login.jsp→dispatcherservlet→hello.jsp, the resulting page is shown in Figure 2-6.
Figure 2-6 Page generated by hello.jsp with Hello tag
This article extracts the free Flying Ideas Book licensing "Tomcat and Java Web Development technology detailed"