• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get a servlet name

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
If I write this compiles error
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HolaServlet extends HttpServlet
{
String nombre;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
nombre = ServletConfig.getServletName();
}
public void service (HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
PrintWriter p = res.getWriter();
res.setContentType("text/html");
p.println("<html>");
p.println("<body>");
p.println("<h2>Hello world</h2>");
p.println("</body>");
p.println("</html>");
p.close();
}
}
What is the correct way for get the servlet name?
Thank you in advance.
 
Meng Tan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use
config.getServletName().
getServletName() is an instance method.
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meng Tan is right. Look at this line
nombre = ServletConfig.getServletName();
You should use config in place of ServletConfig
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic