Solved

webinitparam servlet error

Posted on 2013-01-27
5
592 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 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

20 Experts available now in Live!

Get 1:1 Help Now