Solved

webinitparam servlet error

Posted on 2013-01-27
5
589 Views
Last Modified: 2013-02-05
Below example not working with attached error. Please advise

package com.gp.java;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
@WebServlet(description = "ParameterName desc", urlPatterns = { "/ParameterNameUP" },
    //       initParams={@WebInitParam(name="defaultUser", value="g p")}

initParams = {@WebInitParam(name="defaultUser", value="value1")}

public class InitParamServlet extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {
 
    public InitParamServlet() {
        super();
    }
 
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {/*
         
        PrintWriter pw = response.getWriter();
 
                //
                // Let's obtains parameters name here! 
                //
        Enumeration enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String parameterName = (String) enumeration.nextElement();
            pw.println("Hello Parameter = " + parameterName);
        }
        pw.close();
    */
    	
    response.setContentType("text/html");
    PrintWriter writer=response.getWriter();
    String userName=request.getParameter("name");
    HttpSession session=request.getSession();
    
    ServletContext context= request.getServletContext();
    
    if( userName!="" & userName != null){
    	session.setAttribute("savedUserName", userName);
    	context.setAttribute("savedUserNamec", userName);
    }
    
    writer.println("Hellllooo request"+userName);
    writer.println("Hellllooo session"+(String)session.getAttribute("savedUserName"));
    writer.println("Hellllooo context"+(String)context.getAttribute("savedUserNamec"));
    writer.println("Hellllooo init param has default value of"+getServletConfig().getInitParameter("defaultuser"));
 //   this.getServletConfig().getInitParameter("defaultuser");
    
    
   
    }
 
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    }
}

Open in new window

webInitParam.jpg
0
Comment
Question by:gudii9
  • 3
  • 2
5 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 38825316
I tried as below from web.xml file as well passing initparam value getting null even then also in output

package com.gp.java;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class XmlServlet
 */

public class XmlServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public XmlServlet() {
        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
            System.out.println("hello from get of xml serv");
            PrintWriter writer=response.getWriter();
            writer.println("<h3>hello in html of xml serv</h3>");
      
                  String userNam=request.getParameter("userName");
                  writer.println("heello"+userNam);
                  String[] location=request.getParameterValues("location");
                  writer.println("u at"+location.length+"places");
                    writer.println("Hellllooo init param has default value of"+getServletConfig().getInitParameter("defaultuser"));
      }
      

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            System.out.println("hello from post of xml serv");
            PrintWriter writer=response.getWriter();
            writer.println("<h3>hello in html of xml serv</h3>");
      
                  String userNam=request.getParameter("userName");
                  writer.println("heello"+userNam);
                  String[] location=request.getParameterValues("location");
                  writer.println("u at"+location.length+"places");
      }

}


output shows null

as below
hello in html of xml serv
heelloss u at1places Hellllooo init param has default value ofnull

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 38825431
I tried below example
http://www.roseindia.net/java/javaee6/webInitParamExample.shtml

that worked fine though
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 38826473
I think that there is missing ")" right after this line:
{@WebInitParam(name="defaultUser", value="value1")}
0
 
LVL 7

Author Comment

by:gudii9
ID: 38828746
that error is gone but when i try to run tomcat is giving error like

Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.

Please advise
0
 
LVL 16

Expert Comment

by:Valeri
ID: 38830034
is there any exceptions? Have you setup tomcat in the right way?
Have a look here, probably you have missed something?!
http://www.howtosolvenow.com/2011/07/tomcat-configuration-in-eclipse-indigo/
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now