Welcome
Welcome to <strong>Developer</strong>.

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

First Java application in Websphere Community Edition 2.0

Discuss Java programming technology.

First Java application in Websphere Community Edition 2.0

Postby developer on Thu Jun 26, 2008 7:23 pm

First Java application in Websphere Community Edition 2.0

This is a tutorial for how to implement your first Java application in Websphere CE server.
The example I am using is from Head First Servlet & JSP (Chapter 3).

User selects a value from form.html page and clicks submit button. The value will be passed to a servlet BeerSelect.java and then display the result.

Image

Image

Environment:
Eclipse 3.3, Websphere Community Edition 2.0

Step by Step:
1. Create a project called TestProject.
File -> New -> Web -> Dynamic Web Project

Image

Project name: TestProject
Then click Finish.


2. In WebContent folder, create form.html file.

form.html
Code: Select all
<html>
<body>
<h1 align="center">Beer Selection Page</h1>
   <form method="POST" action="SelectBeer.do">
   Select beer characteristics<p>
   Color:
   <select name="color" size="1">
      <option>light
      <option>amber
      <option>brown
      <option>dark
   </select>
   <br><br>
   <center>
      <input type="SUBMIT">
   </center>
   </form>
</body>
</html>



3. In Java Resources: src folder, create package com.example.web.
Create BeerSelect.java file in this package.

BeerSelect.java

Code: Select all
package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet {
   public void doPost(HttpServletRequest request,
         HttpServletResponse response)
   throws IOException, ServletException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("Beer Selection Advice<br>");
      String c = request.getParameter("color");
      out.println("<br>Got beer color " + c);
   }

}


Pay attention to build folder: it will automatically generate com.example.web.BeerSelect.class file.

Image


4. Change web.xml file.

web.xml
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TestProject</display-name>
  <servlet>
     <servlet-name>Ch3 Beer</servlet-name>
     <servlet-class>com.example.web.BeerSelect</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>Ch3 Beer</servlet-name>
     <url-pattern>/SelectBeer.do</url-pattern>
  </servlet-mapping>
</web-app>



5. This is geronimo-web.xml file.

geronimo-web.xml

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
  <sys:environment>
    <sys:moduleId>
      <sys:groupId>default</sys:groupId>
      <sys:artifactId>TestProject</sys:artifactId>
      <sys:version>1.0</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
  </sys:environment>
  <context-root>/TestProject</context-root>
</web-app>


6. Start server and add project.
Right click server, choose Add and Remove Projects, add TestProject.

7. After project published, open browser and input http://localhost:8080/TestProject/form.html.
Your application is running.
Attachments
WebsphereCE V2.pdf
(235.28 KiB) Not downloaded yet
developer
Site Admin
 
Posts: 37
Joined: Thu Jan 31, 2008 8:26 pm

Return to Java

Who is online

Users browsing this forum: No registered users and 0 guests

cron