Dynamic Web Application Development and deploy webSphere
AGENDA
- Web application development and deployment Steps
– Web application tree structure
– Write (and compile) the Web component code
– Create deployment descriptor
– Build & Deploy Web Application
- Configuring Web application
– Web application deployment descriptor (web.xml file) - Web Application Archive (*.WAR file)
– *.WAR directory structure
– WEB-INF subdirectory
A new project has been created with the standard structure of a Java web application. The WEB-INF/lib directory will later hold all the JAR files that the Java web application requires.
Step 1 : Create a Dynamic Web Project,
To create a Dynamic Web Project, click File menu then select New | Other | Web node | Dynamic Web Project leaf (Eclipse helps you organize your web applications using a type of project called a Dynamic Web Project). In the creation wizard, just type “FirstWebApp” as the project name and click the Finish button.
Step 2: Create file JSP
Right click over the folder “WebContent” and select “New” -> “Other”. Expand “Web” node and select “JSP File” and click Next.
Give the file name as “index.jsp” and click Next. Finally, select the “New JSP File (html)” from Select JSP Templatewindow and click Finish.
Edi the code of your FirstJSP.jsp file, as the modified file shown below. Save your work.
(changes are in bold)
(changes are in bold)
<%@ 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>Insert title here</title></head>
<body>
<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1″>
<title>Insert title here</title></head>
<body>
<h1>Web Application Development Demo!</h1>
<form action=“MyFirstServlet” method=“get”>
Choose Course : <select name=“course”><option value=“Java SE”>Java SE </option><option value=“Java EE”>Java EE</option><option value=“J2ME”>J2ME</option></select>
<input type=“submit” value=“Submit” /></form>
</body>
</html>
<form action=“MyFirstServlet” method=“get”>
Choose Course : <select name=“course”><option value=“Java SE”>Java SE </option><option value=“Java EE”>Java EE</option><option value=“J2ME”>J2ME</option></select>
<input type=“submit” value=“Submit” /></form>
</body>
</html>
Step 3: Creating Servlet Code for processing the Client Request:
Create a servlet. Right click on the folder “src” and select New-> Other. Expand “Web” node and select “Servlet” from Select a wizard. Give the class name for the Servlet (e.g MyFirstServlet), click Next and Finish. Maintain the following data.
Now, your Servlet class file ‘MyFirstServlet’ will be opened in the editor window of your IDE.
Editing your Servlet class ‘MyFirstServlet’
Select the entire code of your Servlet class file from the editor window and delete it & type the following code as shown below. Save your work.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Select the entire code of your Servlet class file from the editor window and delete it & type the following code as shown below. Save your work.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyFirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8″);
PrintWriter out = response.getWriter();
String course=request.getParameter(“course”);
out.print(“You have Selected: “+course);
}}
Create deployment descriptor (web.xml)
The web.xml file provides configuration and deployment information for the Web components that comprise a Web application.
PrintWriter out = response.getWriter();
String course=request.getParameter(“course”);
out.print(“You have Selected: “+course);
}}
Create deployment descriptor (web.xml)
The web.xml file provides configuration and deployment information for the Web components that comprise a Web application.
The web.xml file defines each servlet and JSP page within a web application. It also enumerates enterprise beansreferenced in the web application.
Deployment descriptor (web.xml) contains deployment time & runtime instructions to the Web container
Every web application has to have it.
The file must reside in the WEB-INF directory under the document root of a web application.
Configuration informations, such as Servlet deployment, Servlet parameters are specified in web.xml (Web Applications Deployment Descriptor)
Elements of web.xml: Configuring and Mapping a Servlet
First you configure the servlet. This is done using the
servlet:For each servlet in the web application, there is a <servlet> element.
First you configure the servlet. This is done using the
<servlet>
element. Here you give the servlet a name, and writes the class name of the servlet and defines its physical location of servlet class.servlet:For each servlet in the web application, there is a <servlet> element.
Second, you map the servlet to a URL or URL pattern. This is done in the
<servlet-mapping>
element. In the above example, all URL’s ending in .html
are sent to the servlet.Other possible servlet URL mappings are: /myServlet, /myServlet.do, /myServlet*
servlet-mapping:
Each servlet in the web application gets a servlet mapping. The url pattern is used to map URI to servlets.
session-timeout:
session-timeout:
<session-timeout>: The timeout for a session in minutes.
Welcome-file-list:
<welcome-file>: Specifies the index or home page of your web application.
Note: In our application, we used the default index.jsp instead of creating one.
Initialization Parameters of (web.xml)
Initialization parameters can be shared among Web components (Servlet / JSP) in a web application. (will be discussed in Servlets).
Note: Initialization parameters are configured in the below web.xml file.
Note: Initialization parameters are configured in the below web.xml file.
<servlet>
<servlet-name>greeting</servlet-name>
<servlet-class>GreetingServlet</servlet-class>
<init-param>
<param-name>user</param-name>
<param-value>xyz</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>greeting</servlet-name>
<url-pattern>/greeting</url-pattern>
</servlet-mapping>
Step 4: Test application
Select project | Run as | Run on server
This result :
Step 5 : Deploy websphere
1. Export file war
2. Depoy file war in the websphere
Go to admin websphere : https://x.xx.xx.x:9043/ibm/console/login.do?action=secure
Go to New Application
Sellect file war to deploy, after run install and start
The result after deploy websphere
Good Luck
|
Không có nhận xét nào:
Đăng nhận xét