Home

Architecture

WebApps

Database

Administration

Web Search

News

Java Servers

Web Services

Enterprise

Learning

Contact Us

JumpPoint - Java - EJB - Servlets - JSP - JMS - JNDI - JDBC - RMI

Over 1 billion served...

Nope. We're not going to MacDonalds. But we will be in a very popular place - inside Oracle9i JDeveloper. And our first exploration of JDeveloper will be to build and run a Servlet.

Servlets were one of the first creations in the mix that Sun and the Java community now call J2EE. There are so many Web applications now with Servlets they're probably impossible to count. JDeveloper makes Servlets very easy to create and test. So servlets are a very natural place to start.

JDeveloper provides a short Wizard that will enable us to build and test a servlet in about 20 seconds. OK, let's get started.

First, we're first going create a place to store the new servlet. So we'll create a new Workspace and an empty project. JDeveloper uses Workspaces and Projects to group various kinds of related applications together and to separate them from other applications that you don't want related.


The "File | New" menu: One small step for JDeveloper - Many giant uses for us.

It's easy to create things in JDeveloper easy because you don't have to remember very much. JDeveloper has one central "creation tool " the File | New menu item. It creates many kinds of things, and sometimes more than one at a time. We will start by creating a new project called Project1 under Workspace1.

You can select File | New | Project from the top menu.

First, click on the "File" Menu, and select "New."



Or you can use the keyboard short cut: just press
Ctrl-N
   
The "New" dialog window opens. You use the New dialog window to create all kinds of things in JDeveloper.

To create a WorkSpace, select "
Projects" on the left in the Categories list.

Select "
Workspace" in the right in the "Items" list.



And click "
OK"
   
(2)The New WorkSpace dialog appears. Our first workspace will be called "Workspace1" so just leave every thing as is.

JDeveloper automatically creates an empty project in the workspace if the "Add a New Empty Project is checked.

Just click "
OK"

   
When you finish, you will see Workspace1 show up in the System Navigator window. The first project defaults to "project1."
Note that you can have a number of projects in a Workspace - as shown at the right.


Creating a servlet

Now that we have a place to put it, we'll create a servlet. To do this, we use File | New | WebObject | Servlet.


Again, with the mouse select the "File" menu and click on "New".



This time, select "
Web Objects" as the category. The "Items" menu will display a list of possible objects. Select "HTTP Servlet"

And click "
OK"
   

The "HTTP Servlet" Welcome screen will appears.

Click on "
Next >"

This starts the HTTP Servlet Wizard

The first of Servlet Wizard screens appears. Let's make the following changes to the defaults.

  • Set the package name to "mypackage1" - all lower case.
  • Set the Class name to "MyServlet1"
  • Check the "doPost" implementation method.

Click on "Next >"

You can skip the second and third screens for now.

This sets up a servlet called MyServlet1 with both doGet and a doPost methods.

Run the servlet


Finally we hit the Run | Run menu (or press Shift-F9) to run the project. Doing so deploys the Servlet in a Web application on an internal Oracle9i Application Server.


Now go to your browser and enter the following URL:

http://127.0.0.1:8988/Workspace1-Project1-context-root/servlet/MyServlet1


Your browser should display:

The servlet has received a GET. This is the reply.

Methods in our madness...

Although the servlet, provides both doGet() and doPost() - because that's what we asked for, they do different things. So if you enter a post request, right now the browser displays "The servlet has received a POST." But suppose you want both HTTP get and HTTP post to do the same thing. This is easy to fix. Just remove the code from doGet and replace it with:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

doPost(request, Response);

}

Next in the post method, just change the word "POST" to "request" in the out.println("The servlet has received" line. In this servlet we don't really care whether we are processing a post or a get. The line should now look like this:

out.println("<p>The servlet has received a request. This is the reply.</p>");

And the whole program now looks like this:

package mypackage1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class MyServlet1 extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config) throws ServletException
{
super.init(config);
}



public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, Response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>MyServlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a request. This is the reply.</p>");
out.println("</body></html>");
out.close();
}
}

Now, you can put all your servlet code in the doPost() method and more or less ignore the other method.

On to ==>
Bridge to WebLogic