Here is a guide to help you start building a rigorous web program.
BY Budi Kurniawan
There is an old saying that "hard work is not as good as", "but sometimes it is easy to do. Once you have mastered Struts technology - a framework for building a rigorous web program - your development program is more efficient than now. Regrettably, most of the programmers feel that Struts is a difficult torture process. This article (the first one of the six series) provides a Quick Start for programmers who have used the servlets or JSP page but understand the Struts.
I will teach you how to install and configure Struts, and showcase the specification and deployment descriptive (deployment descriptor) of a small program in the frame. I will carefully explain this procedure in the second part of this sequence.
It is important to build this program to build this program requires a good understanding of Model-View-Controller (MVC) mode in the Java web program. If it is a new concept for you, please read " Almost All Java Web Apps NEED Model 2. "
First, you have to know some basic knowledge about Struts. Struts uses the MODEL 2 architecture based on MVC design mode. For rigorous developers, Model 2 app is an ideal mode because they can develop flexible, scalable and easy maintenance procedures. Even for a simple program, Model 2 is also recommended for use, so there is a framework that can build this program faster and more easily build this program. Apache's Jakarta Struts Project from Apache Software Foundation is this type of framework. This article will help you understand how to use it.
This example is a teaching tool for beginners, so there is no practical performance of Struts. For example, it does not use the Struts tag library or other interesting features, so the code is easy to understand. Moreover, in order to see, it only uses several files. Finally, you will see that Java code is used in the JSP page, which is implemented with beans in practical applications, but if you use this example, you will make the program complicated.
Before you start, you have to install Struts in the machine. If you haven't installed it, please download it now.
Installing Struts Installing Struts is easy, and it does not require a complex configuration. First, download the Struts installation file from http://jakarta.apache.org/site/binindex.html. During the writing of this article, the version 1.0 is a stable version, and the 1.1 version is the BATA version. (I use 1.1-b2 in this series of items)
The Struts program is written in Java, so it requires the use of JDK1.2 or higher. It also requires an XML parser that is compatible with the XML Java API (JAXP) specification. You need to add an XML parser (jaxp.jar / parser.jar / other file) to your ClassPath environment variable. Moreover, if you are using the database, you need to download and install JDBC 2.0 Optional Package Binary from http://java.sun.com/products/jdbc. (If you use JDK1.4, the XML parser and JDBC 2.0 optional package binary will automatically contain inside.)
Also, Struts requires a servlet container that supports Servlet API 2.2 or higher. To find a list of Struts that has been confirmed, please check the install file.
Finally, to make Struts run up, you need to add an environment variable java_home so that it points to the JDK installation root directory and copy the .jar file to the web-inf / lib directory of each program. Register This is an example of a simple registration program that is only available in the MVC mode view and controller. It requires users to enter usernames and passwords before viewing company confidential accounts. The first page of the display is a longin page (see Figure 1). If the registration is successful, the user can see the Main Menu page (see Figure 2), which contain two links: one is exit, the other is to view the company confidential account. If the registration fails, the user will be rebooted to the registration page.
figure 1.
registered
If the user enters the correct username and password, the program creates a session object and there will be a property called Loggedin to be added to this session object. Only when the program can find this loggedin property in the user's session object, users can browse the Main Menu page and the Secret page. All SESSION objects that lack this attribute or invalid will force the user to log in. (The correct username is "john", the password is "123". These values are fixed written in the program to avoid the need to connect to the database or use other storage to complete user authentication.)
When the user exits, his or her session object will be invalid, and the LONGIN page will be redisplayed. When the user clicks on the link to view the company secret, the Secret page will pop up (see Figure 3).
There are three JSP pages that appear in the form of a view in the MVC mode: login.jsp (see Figure 1), mainmenu.jsp (see Figure 2), and viewsecret.jsp (see Figure 3).
figure 2.
Main event
Since all Struts applications are also MODEL 2 applications, they must have something used to act as the controller in the MVC mode (a controller servlet in this example). When using Struts, you don't need to write your own controller servlet. The program has been helped you build, which is also one of the ways to increase development efficiency with Struts.
In the Struts application, the controller servlet is an instance of the org.apache.struts.Action.ActionServlet class. This class is a member of many classes included in the struts.jar file, so you have to distribute this file in each developed program.
image 3.
You found a secret
This controller servlet will send the request to the appropriate view based on the URL in the request. According to the convention, this URI is ending with .do. For example, the URI that is requested to the Secret page is: http: //domain/appname/viewsecret.do, and the URL in the LONGIN page is http: //domain/appname/login.do. (Of course, there can be other information behind the URI)
If you have written a servlet or JSP application, you may guess the servlet mapping in the deployment descriptor to send the request to the ActionServlet instance. That's right, Struts is another version of the servlet / JSP program. The descriptor in the program is displayed in the list 1. These code illustrate the following three points:
In the Action Servlet configuration section, an instance of the org.apache.struts.Action.ActionServlet class is called an action.
All URLs ended with .do are mapped to the Action Servlet.
Login.jsp is a Welcome file, which is called when the URL does not contain a resource name (in other words, when the URL is http: // domain / myApp form) The default file is called. In addition, the deployment descriptor in Listing 1 has no special usage, which simply shows that the Struts application and the general servlet application are very similar.
The request to the resource is sent to the ActionServlet in the application, and then controls control to other objects, which are called action objects. I will explain Action objects and other applications in the second part of this series.
Now you know how to install and configure Struts, and you also understand the registration program project specification and deployment descriptor, then you can click here to download all programs.
About the Author:
Budi Kurniawan is an IT consultant that he specializes in Internet and object-oriented programming, which teaches Java and Microsoft technology. He is a very good Java for the Web with Servlets, JSP, And Ejb; A Developer's Guide To Scalable Solutions (New Riders) Author, he also developed the most popular Java Upload Bean, you can
BrainySoftware.com gets it, many important companies have licensed and used it in the project. Budi contact information is
Budi@brainysoftware.com.