Solved

webinitparam servlet error

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 45
check java version using powershell 13 93
how to see all occupied ports on windows 10 laptop 15 65
eclipse buid path vs tomcat lib path 10 23
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

810 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