Posts

Using SendRedirect() in Java Servlets

Image
How to Redirect User from one page to another in Java   ServletsHome sendRedirect is a method available in HttpServletResponse Interface, allows you to forward the response to another resou rce. Syntax:   response. sendRedirect ( "path" );   A relative URL can be used as a path argument.     Login. html <html> <head> <title></title> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > </head> <body> <form name= "form1" method= "post" action= " SendRedirect " >      Enter UserName:<input type=textbox name= "username" value= "" ><br>      Enter Password:<input type=textbox name= "password" value= "" ><br>      <input type=submit name= "Login" ><br> </form> </body> </html>  The above form will take the user details and redirects ...

ServletContext in Java

Image
   Using ServletContext in Java Servlets     ServletsHome ServletContext in Java Used to set web application level variables. ServletContext is an interface which helps us to communicate with the servlet container. There is only one ServletContext for the entire web application, and the components of the web application can share it. ex: site email, site language, currency, contact number, date format. etc. In web. xml <context-param> <param-name>Email</param-name> <param-value>admin @example.com </param-value> </context-param> This Email value can be accessed by any servlet within the context. In the servlet code, we will write this as.. ServletContext context = getServletContext(); pw. println (context. getInitParameter ( "Email" ); ContextParamServlet. java   package codingzon; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class ContextParamServlet exte...

ServletConfig in Java

Image
Using Servlet Config in Java ServletsHome   ServletConfig in Java: Sets initialization parameters for specific servlet in web. xml ServletConfigDemo. java package codingzon; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletConfigDemo extends HttpServlet { public void init (ServletConfig config) throws ServletException{ super . init (config); } public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //ServletConfig config=; response. setContentType ( "text/html;charset=UTF-8" );         PrintWriter pw = response. getWriter ();            try { pw. println (getInitParameter( "Address" )); } finally { pw. close (); } } } In web.xml, along with servlet, add the parameter details as shown in below given XML code. <se...