...::: Recent Updates :::...

Wednesday, June 20, 2012

Filter

Download Source Code
<!-- Create a servlet filter that logs all access to and from servlets in an application and
prints the following to System.out: or in your terminal or konsole
a. the time
d. the URL of the resource requested
e. the IP address of the visitor-->
<!-- display_Filter.java Filter Class-->
package FilterDisplay;
import java.io.*;
import javax.servlet.*;
import java.util.*;
import javax.servlet.http.*;
public class display_Filter implements Filter
{
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain fc)throws ServletException,IOException
    {
        HttpServletRequest req=(HttpServletRequest)request;
        System.out.println("Requesting URL:-"+req.getRequestURL());
        System.out.println("Host or Ip address:-"+req.getRemoteHost());
        System.out.println("Date Of Accessing Request And Response Time:- "+new Date());
        fc.doFilter(request,response);
    }
    public void init(FilterConfig config){}
    public void destroy(){}
}
<!-- filter is implemented in this Servlet  Simple.java-->
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Simple extends HttpServlet
{
    public void doGet(HttpServletRequest requeset,HttpServletResponse response)throws ServletException,IOException
    {
        response.setContentType("text/html");       
        PrintWriter out=response.getWriter();
        out.println("<html><head><title>fileter Example</title></head><body>");
        out.println("Hello See Your Terminal Please :) ");
        out.println("</body></html>");
        out.close();
    }
}
<!--web.xml file -->
<web-app>
    <servlet>
        <servlet-name>Display</servlet-name>
        <servlet-class>Simple</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Display</servlet-name>
        <url-pattern>/Maulin_Filter</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>Display_Filter</filter-name>
        <filter-class>FilterDisplay.display_Filter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Display_Filter</filter-name>
        <servlet-name>Display</servlet-name>
    </filter-mapping>
</web-app>

Monday, June 18, 2012

generate page not found error

Download Code
<!--Write a servlet that returns a “page not found” error page (404) unless the user supplies a favouriteLanguage request (form) parameter with a value of “Java.” -->

<!-- Check.java -->
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Check extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String language=request.getParameter("language");
        language=language.toLowerCase();
        if(language.equals("java"))   
        {
            out.println("<html><head><title>Error Page :)</title></head><body>");
            out.println("<h1>Welcome dude nice Choice</h1></body></html>");
        }
        else
        {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);       
        }
        out.close();
    }
}

<!-- favlanguage.html -->
<html>
<head>
    <title>Favourate Language</title>
</head>
    <body>
        <form name=f1 method="get" action="/NotFoundError/error_page">
            Enter Your favourate Language:-<input type="text" name="language" /><br>
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>

Jstl "" error Handling :)

You can Download Code from below link
http://www.4shared.com/zip/-krrH0XX/jstlTest.html
<!----- copy the two jar file (jstl.jar,standard.jar)in your webapplication lib directory .you will find this two jar file in /home/maulin/apache-tomcat-7.0.25/webapps/examples/WEB-INF/lib or in wondow$ you will find in webapps\examples\WEB-INF\lib directory -->
<!--- My Directory Structure Show Below you can easily understand it-->




<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
        <title>
                Test
        </title>

</head>
        <body>
                <c:catch var="Maulin">
                        <% int a=0/0; %>
                </c:catch>
                Exception Message:- ${Maulin.message}
        </body>
</html>

Friday, June 1, 2012

Write a Servlet which displays the appropriate PDF file to the client, by looking at a request parameter






Assume that we have got three pdf files for the MCA-1 Syllabus, MCA-2 Syllabus and MCA-3 Syllabus respectively, Now write a Servlet which displays the appropriate PDF file to the client, by looking at a request 
parameter for the year (1, 2 or 3).
Hint: Create a HTML pages which will have a textbox and 
button. The user will enter the values in the textbox – 
either 1,2,3. Pass the value to a servlet page which will 
read the parameter value and display the appropriate 
PDF file. If the option is invalid they just display the 
message invalid semester.
Note: You can use any method – GET or POST

Step 1: Create HTML Page which will have a textbox and button.
which can pass value to servlet page.


Step 2: Create ViewPdf.java servlet file which can interact with pdf files and display it.


Step 3: Create WEB.XML file for Mapping Servlet file.