Ans- No, we cannot give a constructor with arguments in servlet in java because servlet container responsible for creating servlet instance so it call no arguments constructor in servley if you give constructor with arguments then it will give error at time of instantiating
Incorrect Way
package com.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class servlettest
*/
public class servlettest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public servlettest(int a) {
System.out.println("Inside the constructor"+a);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("first_name");
System.out.println("The name is"+name);
RequestDispatcher rd= request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
// super.doGet(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Error will give at time of instantiating
Incorrect Way
package com.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class servlettest
*/
public class servlettest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public servlettest(int a) {
System.out.println("Inside the constructor"+a);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("first_name");
System.out.println("The name is"+name);
RequestDispatcher rd= request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
// super.doGet(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Error will give at time of instantiating
javax.servlet.ServletException: Error instantiating servlet class com.servlet.servlettest org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870) org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685) java.lang.Thread.run(Thread.java:595)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.