aspose file tools*
The moose likes Servlets and the fly likes problem with servlet's init Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with servlet Watch "problem with servlet New topic
Author

problem with servlet's init

devayani Devasthali
Ranch Hand

Joined: Aug 25, 2005
Posts: 40
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

Joined: Mar 22, 2000
Posts: 12825
    
    5
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

Joined: Aug 18, 2005
Posts: 10
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


SCJP 1.2<br />SCWCD 1.3<br />SCBCD 1.3<br />SCDJWS
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with servlet's init