The actual procedures used to develop the application are described below.
1) Creating the Project for the Web Application
5) Verifying the Application Behavior
6) Distributing the Application to the Operating Environment
1) Creating the Project for the Web Application
Select [File] > [New] > [Project] from the menu bar to display the [New Project] wizard.
Select [Web] > [Dynamic Web Project] from the [New Project] wizard, then click [Next].
Check and enter the following setup items:
Setup Items | Setup Content |
---|---|
Project name | WebSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Dynamic Web Module version | 2.5 |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
EAR Membership | Do not select |
Set the information, then click [Next].The Web Module page is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Parameter | Setting |
---|---|
Context Root | WebSample |
Content Directory | WebContent |
Java Source Directory | src |
Generate deployment descriptor | Select |
2-1) Creating the Data Class
Create a data class that stores the country name and total population information, and gets information. To create the data class, select the created project, then right-click to display the context menu. Select [New] > [Class] from the context menu. The [New Java Class] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Parameter | Setting |
---|---|
Source folder | WebSample/src |
Package | sample |
Name | CountryData |
The following source file is generated:
Source file | Description |
---|---|
CountryData.java | Data class |
Implement the processing for storing the country names and total populations. Add the places shown in red below to the source.
Data Class Implementation (CountryData.java)
package sample; public class CountryData { private String countryName; private int totalPopulation; public CountryData(String name, int total) { setCountryName(name); setTotalPopulation(total); } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public int getTotalPopulation() { return totalPopulation; } public void setTotalPopulation(int totalPopulation) { this.totalPopulation = totalPopulation; } } |
Point
After the fields are added, and while the class is in the selected state, [Source] > [Generate Getters and Setters] can be selected from the menu to add getter/setter.
2-2) Creating the Logic Class
To create the logic class, select the created project, then right-click to display the context menu. Select [New] > [Class] from the context menu. The [New Java Class] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Parameter | Setting |
---|---|
Source folder | WebSample/src |
Package | sample |
Name | PopulationRanking |
The following source file is generated.
Source file | Description |
---|---|
PopulationRanking.java | Logic class |
Implement the processing for entering the ranking and returning the country name and total population. Add the places shown in red below to the source.
Logic Class Implementation (PopulationRanking.java)
package sample; public class PopulationRanking { private CountryData[] countries; public PopulationRanking(){ countries = new CountryData[]{ new CountryData("China",1330000000), new CountryData("India",1140000000), new CountryData("U.S.A.",300000000), new CountryData("Indonesia",230000000), new CountryData("Brazil",190000000), new CountryData("Pakistan",160000000), new CountryData("Bangladesh",150000000), new CountryData("Russia",140000000), new CountryData("Nigeria",140000000), new CountryData("Japan",130000000) }; } public CountryData getCountryData(int rank) { --rank; if (rank < 0 || rank >= countries.length) { return null; } return countries[rank]; } } |
3-1) Creating the Pattern for the Servlet Class
To create the servlet class pattern, select the created project, then right-click to display the context menu. Select [New] > [Servlet] from the context menu. The [Create Servlet] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Next].
Parameter | Setting |
---|---|
Web project | WebSample |
Source folder | \WebSample\src |
Java package | sample |
Class name | ServletController |
Superclass | javax.servlet.http.HttpServlet |
Check the following setup items then, without changing anything, click [Next].
Parameter | Setting |
---|---|
Name | ServletController |
URL Mappings | /ServletController |
Check and enter the following setup items. After the following information is set, click [Finish].
Parameter | Setting |
---|---|
Modifiers | Public |
Which method stubs would you like to create? | Constructors from superclass |
The following source file is generated.
Source file | Description |
---|---|
ServletController.java | Servlet class |
Point
With the wizard, web.xml servlet mapping definitions are also added at the same time. As a result, editing of web.xml is not required in this application. If editing of web.xml is required for a reason other than servlet mapping definitions, refer to "2.3.8 Editing the web.xml".
3-2) Implementing the Servlet Class
Add the places shown in red in the created servlet class to the source.
Servlet Class (ServletController.java)
package sample; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletController * */ public class ServletController extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletController() { super(); } /** * @see jHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("ISO-8859-1"); ServletContext sc = getServletContext(); // Get the type of file to be invoked. String mode = request.getParameter("mode"); if (mode != null && mode.equals("top")) { // Invoke the Input page HTML file. RequestDispatcher inRd = getServletContext().getRequestDispatcher("/default.jsp"); inRd.forward(request, response); return; } int rank; // Input check try { rank = Integer.parseInt(request.getParameter("rank")); } catch (NumberFormatException e){ RequestDispatcher errRd = sc.getRequestDispatcher("/error.jsp"); errRd.forward(request, response); return; } PopulationRanking pop = new PopulationRanking(); CountryData country = pop.getCountryData(rank); // Check the obtained value. if(country == null){ RequestDispatcher errRd = sc.getRequestDispatcher("/error.jsp"); errRd.forward(request, response); return; } sc.setAttribute("ranking", country); RequestDispatcher outRd = sc.getRequestDispatcher("/result.jsp"); outRd.forward(request, response); } } |
Point
After the required implementation is coded, right-click on the Java editor, then select [Source] > [Organize Imports] from the context menu. The import statement that is appropriate for the class path that was set in the project can then be inserted.
4) Creating the I/O Page
4-1) Creating the Input Page Pattern
Create the pattern for the JSP file that will be the input page. To create the JSP, select the created project, then right-click to display the context menu. Select [New] > [JSP] from the context menu. The [New JavaServer Page] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Next].
Parameter | Setting |
---|---|
Enter or select the parent folder | WebSample/WebContent |
File name | default.jsp |
Enter the following setup items, then click [Finish] - JSP is displayed.
Parameter | Setting |
---|---|
Use JSP Template | Check |
Name | New JSP file (HTML) |
4-2) Editing the Input Page
Modify the character strings shown in red in the created JSP file. For details on editing a JSP file, refer to "2.3.4.2 Editing the JSP File".
Input Page (default.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Input page</h1> <p>Enter a ranking from 1 to 10.</p> <p></p> <form action="ServletController" method="post"> Rank:<br> <input name="rank" type="text" size="10">Rank<br> <p></p> <input type="submit" value="OK"> <input type="reset" value="Clear"> </form> </body> </html> |
Point
JSP files are associated with the JSP editor by default, but a Web Page editor that enables editing while checking the design and layout can also be used. To select an editor, select the JSP file, then select [Open With] > [Web Page Editor] from the context menu. For details on editing using a Web Page editor, refer to "2.3.5 Graphically Editing the HTML/JSP Files".
4-3) Creating the Output Page Pattern
Use the wizard, in the same way as for the input page, to create the JSP file.
Enter the following values, then click [Finish]. The initial values can be used for the items not shown in the table.
Parameter | Setting |
---|---|
Enter or select the parent folder | WebSample/WebContent |
File name | result.jsp |
4-4) Editing the Output Page
Modify the character strings shown in red in the created JSP file.
Output Page (result.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Output page</h1> Total population ranking${param.rank} rank is "${applicationScope.ranking.countryName}"<br> Total population is ${applicationScope.ranking.totalPopulation} people. <br> <hr> <form action="ServletController" method="post"> <input type="submit" value="Back to input page"> <input type="hidden" name="mode" value="top"> </form> </body> </html> |
4-5) Creating the Error Page Pattern
Use the wizard, in the same way as for the input page, to create the JSP file.
Enter the following values, then click [Finish]. The initial values can be used for the items not shown in the table.
Parameter | Setting |
---|---|
Enter or select the parent folder | WebSample/WebContent |
File name | error.jsp |
4-6) Editing the Error Page
Modify the character strings shown in red in the created JSP file.
Error Page (error.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Error page</h1> The entered ranking is not in the specified range, or a server error occurred.<br> <br> <hr> <form action="ServletController" method="post"> <input type="submit" value="Back to input page"> <input type="hidden" name="mode" value="top"> </form> </body> </html> |
5) Verifying the Application Behavior
5-1) Associate the project with a server
Select a server from the Servers view, then select [Add and Remove Projects] from the context menu.
Select the project from the available projects, then click [Add]. Check that the dynamic Web project has been added to the configured projects.
Check the following setup items, then click [Finish].
Parameter | Setting |
---|---|
Configured projects | WebSample |
If server is started, publish changes immediately | Select |
Point
If the deployment destination server in the Servers view is not registered, then it must be added. For details on how to do that, refer to "6.2.6 Checking Application Behavior".
5-2) Setting Breakpoints
Set a breakpoint at the first line of the doPost() method of the servlet class. To set or delete a breakpoint, double-click the vertical ruler on the left-hand edge of the editor.
Point
Similarly, breakpoints can be set in JSP files using the JSP editor. However, breakpoints can be set only in lines that have JSP tags (breakpoints cannot be set in parts that have HTML tags or similar).
Note
If breakpoints are set in a JSP file, execution is also interrupted for JSP files with the same name in different folders.
5-3) Launching the Server
Select a server from the Servers view, then select [Restart in Debug] from the context menu.
Point
Even if [Restart in Debug] or [Connect(Debug Launch)/Login] is implemented prior to deployment, debugging is not possible.
Check the following information in the Servers view:
Item to Check | Contents to Check |
---|---|
Server state | Debugging |
Server status | Synchronized |
WAR file (WebSample) state | Synchronized |
5-4) Executing the Application
In the Servers view, select the deploying project, then from the context menu, select [Web browser]. The Web browser is started, and the input window is opened.
Parameter | Setting |
---|---|
URL of the input page | http://localhost/WebSample/ |
Enter a ranking in the [Rank:] input field, then click [OK].
Point
The user can select the project, then select [Run As] > [Run on Server] or [Debug As] > [Debug on Server] from the context menu to add the server, add the project, launch the server, and then launch the client all together.
In addition, the Web browser used as the client can be changed by selecting [Window] > [Web Browser] from the menu.
5-5) Debugging the Application
The applications runs and is interrupted at the breakpoints. Check the value of variable by Variables view. Select [Run] > [Step Over] from the menu and check the program state in the Variables view. For debug details, refer to "6.2.6.1 Debugging".
Point
In addition to checking values, values can be changed from the Variables view.
Processing that has been interrupted by the debugger can be restarted by selecting [Run] > [Resume] from the menu. This allows the processing on the server side to complete so that the output page is displayed on the Web browser. Check that the country name and total population are displayed for the entered ranking.
6) Distributing the Application to the Operating Environment
A WAR file must be created in order to distribute the application to the operating environment. Use the server distribution function to distribute the created WAR file to the server.
6-1) Exporting the Application
The WAR file is created with the Export wizard. To launch the Export wizard, select [File] > [Export]. From [Export] wizard, select [Web] > [WAR file].
The WAR Export wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Parameter | Setting |
---|---|
Web project | Specify the dynamic Web application project. |
Destination | Specify the creation destination for the WAR file. |
6-2) Distributing to the Operating Environment
The application can be deployed from a remote environment to the operating environment by logging in to the Interstage Java EE Admin Console at the operating environment and by specifying a local WAR file.