Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with servlet's init

 
devayani Devasthali
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I am writing simple servlets. trying to learn basics of servlets. and having couple of issues. can anybody pls help me out in understanding basics of servlet?
have written one servlet as simpleServlet
code for the same is ----
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Enumeration;

public class simpleServlet extends HttpServlet
{
private int counter;
privateString name ="";
private String language="";

public void init(ServletConfig config)throws ServletException
{

name = config.getInitParameter("name");
language = config.getInitParameter("language");

//System.out.println("got initialization parameter as ---" + name);
Enumeration enum = config.getInitParameterNames();
while(enum.hasMoreElements())
{
System.out.println(enum.nextElement());
}

}
public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
{
counter++;
System.out.println("calling servlet's service method");
System.out.println("--called this servlet ---" + counter + " times" ) ;

System.out.println("---servlet's information---" + getServletInfo());
if(language.equalsIgnoreCase("English"))
response.setCharacterEncoding("Shift_EN");
else
response.setCharacterEncoding("Shift_JIS");

RequestDispatcher rd = request.getRequestDispatcher("/JSP/test2.jsp");
rd.forward(request,response);

}
public void destroy()
{
System.out.println("--servlet is destroyed");
}

}

1)actually, when I called the same servlet through a simle jsp..
it gave me output as language as a parameter name
where as I had set two init-parameter in web.xml as name and language.
is it so that u can set only one parameter . but if that the case then there is no use of getInitParameterNames() method ?
2) if I dont write statement as super.init(config) still it works fine. then why we call super class's init method?
3) how to see the output of destroy method?
4) also nothing was printed with getServletInfo() method .. why? I didnt get this.

please do reply asap.
thanx in advance
regards
Devayani
[ February 15, 2006: Message edited by: devayani Dev ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I dont write statement as super.init(config) still it works fine.

It appears to work fine because your example does not require ServletConfig to be set - instead it uses it locally in init.
If you later expand the functionality you would run into trouble since getServletContext and getServletConfig will return null.
Bill
 
Ralph Ma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)actually, when I called the same servlet through a simle jsp..
it gave me output as language as a parameter name
where as I had set two init-parameter in web.xml as name and language.
is it so that u can set only one parameter . but if that the case then there is no use of getInitParameterNames() method ?


Can you show me your web.xml?

3) how to see the output of destroy method?

When you shutdown the container, the destroy() will be called

also nothing was printed with getServletInfo() method .. why? I didnt get this.

The default implementation of getServletInfo() is to return a empty string. This method should be implemented by yourself
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic