init()
public void init() throws ServletException {
// Statements
}
service()
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
// Statements
}
doGet()
public void doGet(HTTPServletRequest request, HTTPServletResponse response) throws ServletException, IOException {
// Statements
}
destroy()
public void destroy() throws ServletException {
// Statements
}
import javax.servlet.*
import javax.servlet.http.*
public class NamasteDunia extends HttpServlet {
private string message;
public void init() throws ServeletException {
message = "Namaste Dunia";
}
public void doGet(HTTPServeletRequest request, HTTPServeletResponse response) throws ServeletException, IOException {
response.setContentType("text/html");
printWriter out = response.getWriter();
out.println("<h1>"+message+"</h1>");
}
public void destroy() throws ServeletException {
// Statements
}
}
<servlet>
<servlet-name>NamasteDunia</servlet-name>
<servlet-class>NamasteDunia</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NamasteDunia</servlet-name>
<url-pattern>/NamasteDunia</url-pattern>
</servlet-mapping>
RequestDispatcher
rd=request.getRequestDispatcher("resource")
rd.forward(request, response);
RequestDispatcher
rd=request.getRequestDispatcher("resource")
rd.include(request, response);
request.getParameter("name")
=> Get a value from a parameterrequest.getParameterValues()
=> When parameter returns multiple values or appearns more than oncerequest.getParameterNames()
=> Gives complete list of all the parametersCookie c = new Cookie("key", "value");
=> Create Cookiec.setMaxAge(N);
=> Set max ageresponse.addCookie(c);
=> Send into HTTP headerCookie c[] = request.getCookies();
=> Get all the cookies <filter>
<display-name>MyFilter</display-name>
<filter-name>MyFilter</filter-name>
<filter-class>com.MyFilter</filter-class>
</filter>