RSS

Monthly Archives: April 2014

Define JSTL. State types of JSTL.

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.

JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags. It also provides a framework for integrating existing custom tags with JSTL tags.

The JSTL tags can be classified, according to their functions, into following JSTL tag library groups that can be used when creating a JSP page:

1) Core Tags
2) Formatting tags
3) SQL tags
4) XML tags
5) JSTL Functions

 
Leave a comment

Posted by on April 24, 2014 in Material

 

Describe the two situation in which one might use URLs that refer to one’s own site?

There are two possible situations in which you might use URLs that refer to your own site.
The first one is where the URLs are embedded in the Web page that the servlet generates.
These URLs should be passed through the encodeURL method of HttpServletResponse.
The method determines if URL rewriting is currently in use and appends the session information only if necessary.
The URL is returned unchanged otherwise.
Here’s an example:
String originalURL = someRelativeOrAbsoluteURL;
String encodedURL = response.encodeURL(originalURL);
out.println(““);

The second situation in which you might use a URL that refers to your own site is in a sendRedirectcall (i.e., placed into the Locationresponse header).
In this second situation, different rules determine whether session information needs to be attached, so you cannot use encodeURL.
Fortunately, HttpServletResponse supplies an encodeRedirectURL method to handle that case.
Here’s an example:
String originalURL = someURL;
String encodedURL = response.encodeRedirectURL(originalURL);
response.sendRedirect(encodedURL);

If you think there is a reasonable likelihood that your Web application will eventually use URL rewriting instead of cookies, it is good practice to plan ahead and encode URLs that reference your own site.

 
Leave a comment

Posted by on April 24, 2014 in Example

 

Display Multiple Language using Locale.

Localetest.jsp

<%@ page import="java.util.*,java.text.*" %>

<%
	Locale locale;
    DateFormat full;

    try {
      response.setContentType("text/plain; charset=UTF-8");
     

      locale = new Locale("en", "US");
      full = DateFormat.getDateTimeInstance(DateFormat.LONG, 
                                            DateFormat.LONG,
                                            locale);
      out.println("In English appropriate for the US:");
      out.println("Hello World!");
      out.println(full.format(new Date()));
      out.println();

      locale = new Locale("es", "");
      full = DateFormat.getDateTimeInstance(DateFormat.LONG, 
                                            DateFormat.LONG,
                                            locale);
      out.println("En Espa\u00f1ol:");
      out.println("\u00a1Hola Mundo!");
      out.println(full.format(new Date()));
      out.println();

      locale = new Locale("ja", "");
      full = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                            DateFormat.LONG,
                                            locale);
      out.println("In Japanese:");
      out.println("\u4eca\u65e5\u306f\u4e16\u754c");
      out.println(full.format(new Date()));
      out.println();

      locale = new Locale("zh", "");
      full = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                            DateFormat.LONG,
                                            locale);
      out.println("In Chinese:");
      out.println("\u4f60\u597d\u4e16\u754c");
      out.println(full.format(new Date()));
      out.println();

      locale = new Locale("ko", "");
      full = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                            DateFormat.LONG,
                                            locale);
      out.println("In Korean:");
      out.println("\uc548\ub155\ud558\uc138\uc694\uc138\uacc4");
      out.println(full.format(new Date()));
      out.println();

      locale = new Locale("ru", "");
      full = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                            DateFormat.LONG,
                                            locale);
      out.println("In Russian (Cyrillic):");
      out.print("\u0417\u0434\u0440\u0430\u0432\u0441\u0442");
      out.println("\u0432\u0443\u0439, \u041c\u0438\u0440");
      out.println(full.format(new Date()));
      out.println();
    }
    catch (Exception e) 
	{
      out.println(e);
    }
%>

 
Leave a comment

Posted by on April 17, 2014 in Example

 

Servlet Specification 3.0

Servlet Specification 3.0
Download

 
Leave a comment

Posted by on April 17, 2014 in Material

 

Struts : Simple Demo

Steps to generate struts web Application in NetBeans

Sourcecode : Download

 
Leave a comment

Posted by on April 11, 2014 in Example

 

Deploy war file in tomcat

1) Run the tomcat service using http://localhost:9090/
2) Go to Manager App.
3) Under the War file to deploy,select the location for your war file.
warfile
4) That’s it. your web application is deployed on server.

 
Leave a comment

Posted by on April 10, 2014 in Example

 

Generate war file from tomcat webapps folder

Create a war file for your web application or your project.
To create a war file :

1) open the command prompt
2) Go to your project directory or web application directory
For Example : C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\Myproject
here Myproject is a directory for which we want to generate war file.
3) Type the following command :
jar -cvf myapp.war *
(Which means, “compress everything in this directory into a file named myapp.war” (c=create, v=verbose, f=file) )
4) so war file is generated on that location.
FINISH.

 
Leave a comment

Posted by on April 10, 2014 in Example

 

Configuring Filters to Work with RequestDispatcher

Version 2.3 of the servlet specification only allowed us to configure filters for requests that came directly from the client.
Filters did not apply to requests that were made as a result of the forward or include methods of RequestDispatcher.
Version 2.4 introduced a new deployment descriptor element, dispatcher, which allows us to configure filters to do just that.
The optional dispatcher element must be placed inside the filter-mapping element after the url-pattern or servlet-name element.
Possible values for the dispatcher element is REQUEST,INCLUDE,FORWARD,ERROR.

<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
 
Leave a comment

Posted by on April 9, 2014 in Example

 

Modifying the response through filter

Here through filter, we wnat to change the company name from ABC to XYZ.

StringReplacement.jsp

String Replacement filter example 
</br>
ABC comapany.

web.xml

    <filter>
        <filter-name>test1</filter-name>
        <filter-class>ModificationFilter</filter-class>
     </filter>
     <filter-mapping>
      <filter-name>test1</filter-name>
      <url-pattern>/StringReplacement.jsp</url-pattern>
    </filter-mapping>

ModificationFilter.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ModificationFilter implements Filter
{
	public void init(FilterConfig fc) throws ServletException
	{
	}
	public void doFilter(ServletRequest req,ServletResponse res,FilterChain fc) throws ServletException,IOException
	{
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) res;
			
		ReplacementFilter rf=new ReplacementFilter(response);
		fc.doFilter(request,rf);
		
		String ns=rf.toString();
		PrintWriter out=response.getWriter();
		out.write(ns);
	}
	public void destroy()
	{
	}
}

ReplacementFilter.java

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class ReplacementFilter extends HttpServletResponseWrapper
{
	StringWriter sw;
	
	ReplacementFilter(HttpServletResponse res)
	{
		super(res);
		sw=new StringWriter();
	}
	
	public PrintWriter getWriter()
	{
		return (new PrintWriter(sw));
	}
	public ServletOutputStream getOutputStream()
	{
		return (new ServletOutputStream()
		{
		public void write(int c)
			{
				sw.write(c);
			}
		});
	}
	public String toString()
	{
		
		StringBuffer sb = sw .getBuffer(); 
		String finalstring = sb.toString();
		
		int index= finalstring.indexOf("ABC");
		String t=finalstring.substring(0,index -1);
		String l=finalstring.substring(index+3);
		
		StringBuilder value=new StringBuilder(t);
		value.append("XYZ");
		value.append(l);
		value.toString();
		
		return (value.toString());
	}
}

 
Leave a comment

Posted by on April 9, 2014 in Example

 

Blocking the Response through filter

Suppose if we have two pages test.jsp and test1.jsp.
Now if any user directly run test.jsp then we want to allow the access to the test.jsp but
if user come to test.jsp page via test1.jsp then we want to block the response.
To implement these follow below mentioned steps :

Test.jsp

This is my Test page.

Test1.jsp

<a href="Test.jsp"> Test.jsp </a>

BannedSiteDemo.java

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class BannedSiteDemo implements Filter 
{
	private String bs ;
	public void init(FilterConfig config) throws ServletException 
	{
			bs = config.getInitParameter("sitename");
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException 
	{
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse) response;
		
		String ref=req.getHeader("referer");
		if(ref==null)
		{
			chain.doFilter(request,response);
		}
		else
		{
			if (ref.equals(bs))
			{
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();
	   
				out.println
				(
			   "<HTML>\n" +
			   "<HEAD><TITLE>Access Prohibited</TITLE></HEAD>\n" +
			   "<BODY BGCOLOR=\"WHITE\">\n" +
			   "<H1>Access Prohibited</H1>\n" +
			   "Sorry, access from or via " + bs + "\n" +
			   "is not allowed.\n" +
			   "</BODY></HTML>");
			}
	    
		}
	}
    public void destroy() {}

}
 <filter>
        <filter-name>BannedSiteDemo</filter-name>
        <filter-class>BannedSiteDemo</filter-class>
        <init-param>
            <param-name>sitename</param-name>
            <param-value>http://localhost:9090/filter/Test1.jsp</param-value>
        </init-param>
    </filter>
	<filter-mapping>
      <filter-name>BannedSiteDemo</filter-name>
      <url-pattern>/Test.jsp</url-pattern>
    </filter-mapping>
 
Leave a comment

Posted by on April 9, 2014 in Example