Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:44:07+00:00 2026-06-07T17:44:07+00:00

I am absolutely new in Servlet technology and this is absolutely basic question, but

  • 0

I am absolutely new in Servlet technology and this is absolutely basic question, but I am all confused by the tutorials that are all too complicated for me.

I have a new servlet HelloWorldServlet. In web.xml, I have this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
     "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
     <servlet>
         <servlet-name>HelloWorldServlet</servlet-name>
         <servlet-class>cz.hello.HelloWorldServlet</servlet-class>
     </servlet>

     <servlet-mapping>
         <servlet-name>HelloWorldServlet</servlet-name>
         <url-pattern>/HelloWorldServlet</url-pattern>
     </servlet-mapping>
</web-app>

HelloWorldServlet.scala (I prefer scala to java) looks like this

package cz.hello

import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

 class HelloWorldServlet extends HttpServlet {
    override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
        resp.setContentType("text/plain")
        resp.getWriter.println("Hello, world")
    }
}

So far so good, servlet is loaded using Jetty, I am happy, I can see “hello world” on http://localhost:8080/HelloWorldServlet.

Now, I want the servlet to be able to react to GET requests to, say, http://localhost:8080/HelloWorldServlet/hello and http://localhost:8080/HelloWorldServlet/goodbye and to both of them differently. For example like (pseudocode)

    override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
        resp.setContentType("text/plain")
        if (req.isAddress("/hello") {
            resp.getWriter.println("Hello, world")
        } else {
            resp.getWriter.println("Goodbye, world")
        }
    }

How can acchieve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:44:08+00:00Added an answer on June 7, 2026 at 5:44 pm

    First of all, if you want to react to POST requests, you should implement the doPost method, instead of the doGet.

    Second, I would advise you to think about handling each URL in a different servlet, unless your code will be as simple as the example you provided.

    Chances are your code will get more complex when developing real-world applications, so it would be much cleaner if you separate the responsibilities into two servlets. If you agree with this approach, it is just a matter of creating another <servlet> and another <servlet-mapping> object in your web.xml, like follows:

    <web-app>
     <servlet>
         <servlet-name>HelloWorldServlet</servlet-name>
         <servlet-class>cz.hello.HelloWorldServlet</servlet-class>
     </servlet>
     <servlet>
         <servlet-name>GoodbyeWorldServlet</servlet-name>
         <servlet-class>cz.hello.GoodbyeWorldServlet</servlet-class>
     </servlet>
    
     <servlet-mapping>
         <servlet-name>HelloWorldServlet</servlet-name>
         <url-pattern>/hello</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
         <servlet-name>GoodbyeWorldServlet</servlet-name>
         <url-pattern>/goodbye</url-pattern>
     </servlet-mapping>
    </web-app>
    

    This way, requests to /hello will be handled by HelloWorldServlet, and requests to /goodbye will be handled by GoodbyeWorldServlet. Now it is just a matter of defining whether GET or POST makes more sense for you and implementing the corresponding methods(doGet or doPost, or both) at your servlets.

    Your idea (comparing the contents of the URL inside the servlet) also works, but is not a good design, since you might end up with a huge if/then/else chain, which sounds like a bad idea in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m absolutely new in Matlab, so I think it is a dumb question, but
I must admit that I'm absolutely new to Expression Trees in C#, but currently
Firstly apologies if this is a really simple question but Git is absolutely brand
First of all, I am absolutely new to WP. I am hoping to build
Instead of absolutely positioning (I have to do that, don't ask why) elements over
All, Please forgive my ignorance of C#/.NET, I am absolutely new to both (mostly
I am absolutely new to javascript, so please bear with me. I have 50
I'm absolutely new to GWT, java and eclipse, but I'm an experienced MS programmer.
I have an XBMC setup that I absolutely love at home. Now, I've been
This is driving me absolutely nuts. I created a new WAP project in VS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.