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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:39:56+00:00 2026-06-01T08:39:56+00:00

Hi if someone could look at my code and maybe see something I am

  • 0

Hi if someone could look at my code and maybe see something I am not, this is for a lab for a class assignment of mine. Thank you!

I am programming a simple servlet in a dynamic web project using eclipse. One of the labs requirements is to grab an init parameter from the web.xml in a Servlet.

When I try to get the value of an init-param in my servlet. It keeps returning null.

Anyone see anything wrong that I am not:

im using command:

this.getServletConfig().getInitParameter("title");

In class ConvertServlet that is in a00730628.controller package with in the doPost function. I also tried from the init function but got null there to, so I am thinking there is an error in my xml. I am using version 3.0 and Tomcat 7.26

And here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>lab10</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>



<servlet>

<servlet-name>ConvertServlet</servlet-name> 

<servlet-class>a00730628.controller.ConvertServlet</servlet-class>



<init-param>

  <param-name>title</param-name>

  <param-value>Temperature Converter Result</param-value>

</init-param>

</servlet>

</web-app>

Edit added my Servlet Code:

package a00730628.controller;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import a00730628.util.TemperatureConverter;
import a00730628.view.Lab10Html;

/**
 * Servlet implementation class ConvertServlet
 */
@WebServlet("/convert")
public class ConvertServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final int CEL_2_FAR = 0;
    private static final int FAR_2_CEL = 1;

    private String resultTitle = "";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConvertServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().println("<a href='index.html'>Please make a post request from here</a>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try {
            resultTitle = this.getServletConfig().getInitParameter("title");
            if (resultTitle == null) {
                response.sendError(500, "Title param is null ... wa wa wa");
                return;
            }
            int type = Integer.parseInt(request.getParameter("type"));
            double number = Double.parseDouble(request.getParameter("number"));
            double converted;
            String result = "";
            switch (type) {
                case CEL_2_FAR:
                    converted = TemperatureConverter.celsiusToFarenheit(number);
                    result = String.format("%f celsius = %f farenheit", number, converted);
                    break;
                case FAR_2_CEL:
                    converted = TemperatureConverter.farenheitToCelsius(number);
                    result = String.format("%f farenheit = %f celsius", number, converted);
                    break;

            }
            response.getWriter().print(Lab10Html.getLab10Html(resultTitle, result));
        } catch (NumberFormatException e) {
            response.sendError(400, "Please enter a number like 42. Number format error: "+ e.getMessage());
        }


    }

}
  • 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-01T08:39:57+00:00Added an answer on June 1, 2026 at 8:39 am

    You are using the annotations in the servlet. To add the init parameter using annotations,follow the following syntax and remove the servlet configarations in web.xml.

    @WebServlet(name = "convert", urlPatterns = {"/convert"},
      initParams = {@WebInitParam(name="title", value="Temperature Converter Result")}
    )
    

    If you don’t want to use annotations remove @WebServlet("/convert") from the servlet class.

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

Sidebar

Related Questions

I'm wondering if someone could look over my code. I'm trying to pass a
I was hoping someone could have a look and tidy this up for me;
I have the following code and I was wondering if someone could look at
Hi I am wondering if someone could take a look at my code and
Could someone take a quick look, why the text in the dark grey isn't
Could someone take a look at the following link of a work in progress
If someone could point me to some good resources on building a stateless web
I wonder if someone could help me figure out how to parse a string
I was wondering if someone could tell me the best way to call a
I wonder if someone could be kind enough to explain the function.prototype thingie (thingie!!??)

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.