Problem solve Get help with specific problems with your technologies, process and projects.

Getting started with J2EE for SAP developers

Coming from the SAP world, you may not have a lot of experience with object-oriented technology and the Internet.

Coming from the SAP world, you may not have a lot of experience with object-oriented technology and the Internet....

Learning J2EE can become overwhelming because there are several hurdles to clear just to get started. One way of learning the J2EE is to dig in and start writing some code. Of course, it will take a while to get the hang of it and the first 10 lines of code make take a few days to fully understand, but it does get easier over time. With this in mind, let's write some code.

Just like Java 2 Standard Edition, J2EE starts with the Java syntax and adds certain objects (classes) that are specific to enterprise level computing and the Internet. One of the most common tasks that the J2EE has solved is how to respond to a client request from a browser. When a user types the URL http://www.mycompany.com/WelcomeServlet?name=Joe into a browser, you would like to have code that executes and returns an HTML response welcoming Joe to the Web page. In this case, there is a parameter named "name" that is part of the URL.

The Java Servlet API (part of the J2EE) includes a class called HttpServlet. This class contains functions, or as we call them in Java, methods, to handle client requests. To create a Servlet that will return a welcome message, we will create a new Java class that inherits from HttpServlet. By inheriting from HttpServlet, our new class WelcomeServlet, gets to use all of HttpServlet methods. If you are familiar with Object Oriented Programming you will recognize this concept. HttpServlet has a method called service() that gets called whenever a client request comes in. Of course, we will have to associate the URL with this servlet but that is another topic.

It is important to read the name of the person visiting the web page from the URL typed in. Luckily there is another class designed for that, HttpServletRequest. And lastly we will use another class to return our response back to the user, HttpServletResponse. In all, our WelcomeServlet code looks like this:

public class WelcomeServlet extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name");

PrintWriter out = response.getWriter();
out.println("Hello " + name);
}
}

While this code does not do very much, you can take away several key points.

  1. One way to write server-side Java code that responds to client requests from a browser is to create a new class that inherits from HttpServlet and then override the service() method.

  2. One way to read parameters from a URL is to use the HttpServletRequest class and call the getParameter method.

  3. One way to send information back to the client's browser is to use the HttpServletResponse class and call the getWriter() method.

For those of you that are starting to work with the J2EE, I hope this whets your appetite and inspires you to take this simple example and extend it. Go to http://java.sun.com/j2ee/sdk_1.2.1/, download the software and make sure you download the documentation. Remember, it will be slow in the beginning and you will make mistakes in your syntax and choice of Java classes to use. That's ok, just try not to make the same mistakes twice and your knowledge of the J2EE will grow.


Jeff Marin is director of training and education for Gamma Enterprise Technologies Inc.


This was last published in March 2002

Dig Deeper on SAP Java and J2EE

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close