Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

my first servlet

Posted on 2005-05-09
2
Medium Priority
?
238 Views
Last Modified: 2010-05-18
i am really very overwhelmed and i can't find something on the internet to take me step by step. i found something in java.sun.com
http://java.sun.com/developer/onlineTraining/J2EE/Intro2/servlet/servlet.html

but still not easy enough!

1. i am not sure where 2 put the xxx.html file
2. i am not sure where 2 put the xxx.java file
3. i am not sure if its ok 2 use an edito like eclipse 2 create the xxx.java file + the xxx.html file
4. i am not sure how 2 compile the xxx.java file << main issue
5. i got the run.bat running correctly but i don't have the file deployer.jar!

i will paste my code:
1. sallam.html:
<HTML>
  <HEAD>
    <TITLE>
      Sallam
    </TITLE>
  </HEAD>
  <BODY BGCOLOR="WHITE">
    <BLOCKQUOTE>
      <H3>Sallam</H3>
    </BLOCKQUOTE>
  </BODY>
</HTML>

2. sallamServlet.java:
package deploy.FirstServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** This servlet Says Sallam to the user in a HTML page. */

public class sallamServlet extends HttpServlet {
      /**
       * This method will be called by the Servlet Container when
       * this servlet is being placed into service.
       * @param config - the ServletConfig object that
       * contains configutation information for this servlet.
      */
      
      public void init(ServletConfig config) {
            System.out.println("sallamServlet: init()");
      }
      
      /**
       * This method handles the HTTP GET requests for this servlet.
       * It says Sallam to the user in an HTML page.
       * @param request - object that contains the request the
       * client has made of the servlet.
       * @param response - object that contains the response the servlet
       * sends to the client.
       * @exception java.io.IOException - if an input or output error is
       * detected when the servlet handles the GET request.
       * @exception ServletException - if the GET request could not be handled.
       */
      
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
            
            
            // write the response to be displayed
            // set the response type
            response.setContentType("text/html");
            
            // obtain the writer to write the response
            PrintWriter out = response.getWriter();
            
        // write the page title
          out.println("<HTML><HEAD><TITLE>");
          out.println("Servlet Example - Sallam");
          out.println("</TITLE></HEAD><BODY>");

          // write sallam
          out.println("<H1>Sallam</H1>");
          
          // close the page
          out.println("</BODY></HTML>");

          // close the writer
          out.close();
      }//doGet
      
      
      /** This method will be called by the Servlet Container when this servlet
        ** is being taken out of service.
        */
      public void destroy() {
            System.out.println("sallamServlet: destroy()");
      }//destroy
      
      }//Class sallamServlet
0
Comment
Question by:almaha
2 Comments
 
LVL 29

Accepted Solution

by:
bloodredsun earned 340 total points
ID: 13958487
1. i am not sure where 2 put the xxx.html file
Create a web application called Sallaam (create folder called "Sallam" <tomcat_home./webapps, go to http://localhost:8080/manager/html), scroll down to "Install" and type "sallam" in the text box named "WAR or Directory URL:"
Put the html files in this directory.

2. i am not sure where 2 put the xxx.java file
Create a series of folders in "Salaam" called "WEB-INF/classes/deploy/FirstServlet" and put your servlet class in there

3. i am not sure if its ok 2 use an edito like eclipse 2 create the xxx.java file + the xxx.html file
Yes, it's fine

4. i am not sure how 2 compile the xxx.java file << main issue
In eclipse, CTRL-B is the hot key combination to call build

5. i got the run.bat running correctly but i don't have the file deployer.jar!
You don't need it. What you do need is a web.xml file (the deployment descriptor) that will hold your servlet mappings and allow your servlet to be accessable from the url.

This is a set of fairly basic but essential steps. If you're not sure where to put html files in tomcat, I'm not sure that your knowledge level is ready for servlet programming. Learn more about how to use Tomcat, with HTML and JSPs first and then when you are comfortable with that, try some servlets. Learning more about your IDE and how to compile java classes would also be a good thing.

There's not much more advice I can give you, apart from taking you through it step-by-step in a hand holding manner, and unfortunately I don't have the time. I suggest that you go here http://www.jsptut.com/ and do the tutorials on jps and then when you've done that, go here http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ for the servlet ones.
0
 

Author Comment

by:almaha
ID: 13958932
I am not using TomCat alone. I am using JBoss.
I know i have 2 have basic knowledge about using JBoss first.
I am finding it very difficult to understand how servlets work and how 2 set everything up.
That's what i needed, a good site to start understanding about servlets and JBoss, i guess.....
Eclipse is not making it easier by asking me about all the package, file, class info. I mean all i have is a simple html and a java file. i only want to make a successful HelloWorld servlet working :s
Thanks,
Maha
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
Use this step by step method when setting up QuickBooks Online. They will allow you to explore the various features of the advanced settings available to you.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question