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 8569935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:26:54+00:00 2026-06-11T18:26:54+00:00

I’m learning servlets 3.0 through a set of code examples and the purpose of

  • 0

I’m learning servlets 3.0 through a set of code examples and the purpose of many of the methods is not making any sense to me. Except the service method.

The output is “Hello from MyServlet”. But, what’s with all the other methods?

@WebServlet(name = "MyServlet", urlPatterns = { "/my" })
public class MyServlet implements Servlet {

    What is the line below trying to do? 
private transient ServletConfig servletConfig;

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    this.servletConfig = servletConfig;
}

@Override
public ServletConfig getServletConfig() {
    return servletConfig;
}

@Override
public String getServletInfo() {
    return "My Servlet";
}


     //This is the only method that makes sense to me. All the others, I have no 
        idea why they are in here.
@Override
public void service(ServletRequest request, ServletResponse response)
        throws ServletException, IOException {
    String servletName = servletConfig.getServletName();
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.print("<html><head></head>" + "<body>Hello from " + servletName
            + "</body></html>");
}

@Override
public void destroy() {
}

}

  • 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-11T18:26:55+00:00Added an answer on June 11, 2026 at 6:26 pm

    I’m not sure what tutorials/examples you’ve read, but you should rather be extending the HttpServlet abstract class, not implementing the Servlet interface. You indeed don’t directly need any of them. The HttpServlet has already implemented all the necessary boilerplate for you.

    All with all, this is what you minimally need:

    @WebServlet("/my")
    public class MyServlet extends HttpServlet {
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String servletName = getServletConfig().getServletName();
            response.setContentType("text/html");
            response.getWriter().print("<html><head></head>" 
                + "<body>Hello from " + servletName + "</body></html>");
        }
    
    }
    

    (even though I understand that this is just a learning exercise, I’d like to note that emitting HTML in a servlet this way is a poor practice, there JSP should be used for)

    As to what they all did, well, the init() offers you the possibility to perform initialization based on servlet configuration during servlet’s construction. The destroy() offers you the possibility to perform cleanup during servlet’s destroy. The property and those getter methods are just necessary in order to satisfy the Servlet interface’s contract. Note that none of them are explicitly specific to Servlet 3.0. They existed already in older versions.

    See also:

    • Our servlets wiki page – contains more enlightening hello world examples
    • How do servlets work? Instantiation, sessions, shared variables and multithreading
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.