Search This Blog

Showing posts with label servlet and jsp. Show all posts
Showing posts with label servlet and jsp. Show all posts

Thursday, 9 January 2014

how to write data into PDF using Servlet

Example to write data into PDF using servlet

Let's see the simple example of writing data into PDF using servlet. In this example, we have mentioned the content type application/pdf that must be specified to display data in the PDF format.
ServletPDF.java
  1. package com.javatpoint;  
  2. import java.io.*;  
  3. import java.util.*;  
  4. import javax.servlet.*;  
  5. import javax.servlet.http.*;  
  6. import com.darwinsys.spdf.PDF;  
  7. import com.darwinsys.spdf.Page;  
  8. import com.darwinsys.spdf.Text;  
  9. import com.darwinsys.spdf.MoveTo;  
  10.   
  11. public class ServletPDF extends HttpServlet {  
  12.   
  13. public void doGet(HttpServletRequest request,  
  14.         HttpServletResponse response) throws IOException {  
  15.   
  16. PrintWriter out = response.getWriter();  
  17. response.setContentType("application/pdf");  
  18.   
  19. response.setHeader("Content-disposition","inline; filename='rajvishwakarma15@blogspot.pdf'");  
  20.   
  21. PDF p = new PDF(out);  
  22. Page p1 = new Page(p);  
  23. p1.add(new MoveTo(p, 200700));  
  24. p1.add(new Text(p, "rajvishwakarma15@blogspot.com"));  
  25. p1.add(new Text(p, "by Raj Vishwakarma"));  
  26.           
  27. p.add(p1);  
  28. p.setAuthor("Ian ");  
  29.   
  30. p.writePDF();  
  31.   
  32. }  
  33. }  

Ref. javatpoint.com

Expression Language (EL) in JSP

Expression Language (EL) in JSP


There are many implicit objects, operators and reserve words in EL.The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application etc.
It is the newly added feature in JSP technology version 2.0.
EL helps you to save your programming time.

Syntax for Expression Language (EL)

  1. ${ expression }  

Implicit Objects in Expression Language (EL)

There are many implicit objects in the Expression Language. They are as follows:
Implicit ObjectsUsage
pageScopeit maps the given attribute name with the value set in the page scope
requestScopeit maps the given attribute name with the value set in the request scope
sessionScopeit maps the given attribute name with the value set in the session scope
applicationScopeit maps the given attribute name with the value set in the application scope
paramit maps the request parameter to the single value
paramValuesit maps the request parameter to an array of values
headerit maps the request header name to the single value
headerValuesit maps the request header name to an array of values
cookieit maps the given cookie name to the cookie value
initParamit maps the initialization parameter
pageContextit provides access to many objects request, session etc.

Simple example of Expression Language that prints the name of the user

In this example, we have created two files index.jsp and process.jsp. The index.jsp file gets input from the user and sends the request to the process.jsp which in turn prints the name of the user using EL.

index.jsp

  1. <form action="process.jsp">  
  2. Enter Name:<input type="text" name="name" /><br/><br/>  
  3. <input type="submit" value="go"/>  
  4. </form>  

process.jsp

  1. Welcome, ${ param.name }  

Example of Expression Language that prints the value set in the session scope

In this example, we printing the data stored in the session scope using EL. For this purpose, we have used sessionScope object.

index.jsp

  1. <h3>welcome to index page</h3>  
  2. <%  
  3. session.setAttribute("user","sonoo");  
  4. %>  
  5.   
  6. <a href="process.jsp">visit</a>  

process.jsp

  1. Value is ${ sessionScope.user }  

Example of Downloading file from the server using JSP

Example of Downloading file from the server using JSP

In this example, we are going to download the jsp file. But you may download any file. For downloading the file from the server, you should specify the content type named APPLICATION/OCTET-STREAM.

index.jsp

This file provides a link to download the jsp file.
  1. <a href="download.jsp">download the jsp file</a>  

download.jsp

In this example, we are downloading the file home.jsp which is located in the e: drive. You may change this location accordingly.
  1. <%    
  2.   String filename = "home.jsp";   
  3.   String filepath = "e:\\";   
  4.   response.setContentType("APPLICATION/OCTET-STREAM");   
  5.   response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   
  6.   
  7.   java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);  
  8.             
  9.   int i;   
  10.   while ((i=fileInputStream.read()) != -1) {  
  11.     out.write(i);   
  12.   }   
  13.   fileInputStream.close();   
  14. %>   



Ref. Javatpoint.com