Response Header Download
Monthly Archives: February 2013
Servlet that Generate JPEG Images
Servlet that Generate JPEG Images
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.servlet.*; import javax.servlet.http.*; public class ImageDevelop extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); OutputStream out=response.getOutputStream(); try { BufferedImage image=new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB); Graphics2D g=(Graphics2D)image.getGraphics(); g.setBackground(new Color(999)); g.drawString("Test",50,50); g.drawLine(30, 30, 30, 150); g.drawLine(30,150, 150, 150); g.drawLine(150, 150, 150, 30); g.drawLine(150, 30, 30, 30); ImageIO.write(image, "jpg", out); } finally { out.close(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
Example of Refresh Response Header
Example of refresh header
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Refresh extends HttpServlet { static int i; static { int i=0; } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { response.setHeader("Refresh", "1"); //or //response.setIntHeader("Refresh", 1); out.print("<h1>Value Of i : " + i + "<h1>"); i++; } finally { out.close(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
Servlet Practical Assignment -2
Servlet Practical Assignment -2
1) Develop a Servlet which will display different images based on the name of the html file.
2) Write a servlet that builds an Excel spreadsheet that display student name,mark1, mark2, mark3 and sum of three subjects mark.
3) Suppose we have three pdf files for the MCA-1 result, MCA-2 result and MCA-3 result 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). (use response.sendRedirect() method )
4) Write a Servlet to show special welcome message for users who are not accessing Internet Explorer.
5) Write a Servlet to show special message when user comes from google, yahoo and MSN Search engine.
6) Write a servlet that returns a “page not found” error page (404) unless the user supplies a favourite Language request (form) parameter with a value of “Java.”
Example of User-Agent request header
WrongDestination.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that sends IE users to the Netscape home page and * Netscape (and all other) users to the Microsoft home page. */ public class WrongDestination extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { String userAgent = request.getHeader("User-Agent"); if ((userAgent != null) && (userAgent.indexOf("MSIE") != -1)) { response.sendRedirect("http://home.netscape.com"); } else { response.sendRedirect("http://www.microsoft.com"); } } }
Example of sendError() method of HttpServletResponse interface
sendErrorEx.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class sendErrorEx extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("This won't be seen if sendError() is called"); if (req.getParameter("param") == null) { // res.sendError(res.SC_BAD_REQUEST, "param needed"); OR res.sendError(401, "param needed"); } } }
First time run these program with these URL:
http://localhost:8080/sendErrorEx?param=abc
Second time run these program with these URL:
http://localhost:8080/sendErrorEx
Second time it gives you error message when you do not pass the param parameter.
Servlet program that shows all standard CGI variables
ShowCGIVariables.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Creates a table showing the current value of each * of the standard CGI variables. */ public class ShowCGIVariables extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String[][] variables = { { "AUTH_TYPE", request.getAuthType() }, { "CONTENT_LENGTH", String.valueOf(request.getContentLength()) }, { "CONTENT_TYPE", request.getContentType() }, { "DOCUMENT_ROOT", getServletContext().getRealPath("/") }, { "PATH_INFO", request.getPathInfo() }, { "PATH_TRANSLATED", request.getPathTranslated() }, { "QUERY_STRING", request.getQueryString() }, { "REMOTE_ADDR", request.getRemoteAddr() }, { "REMOTE_HOST", request.getRemoteHost() }, { "REMOTE_USER", request.getRemoteUser() }, { "REQUEST_METHOD", request.getMethod() }, { "SCRIPT_NAME", request.getServletPath() }, { "SERVER_NAME", request.getServerName() }, { "SERVER_PORT", String.valueOf(request.getServerPort()) }, { "SERVER_PROTOCOL", request.getProtocol() }, { "SERVER_SOFTWARE", getServletContext().getServerInfo() } }; String title = "Servlet Example: Showing CGI Variables"; String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<CENTER>\n" + "<H1>" + title + "</H1>\n" + "<TABLE BORDER=1>\n" + " <TR BGCOLOR=\"#FFAD00\">\n" + " <TH>CGI Variable Name<TH>Value"); for(int i=0; i<variables.length; i++) { String varName = variables[i][0]; String varValue = variables[i][1]; if (varValue == null) varValue = "<I>Not specified</I>"; out.println(" <TR><TD>" + varName + "<TD>" + varValue); } out.println("</TABLE></CENTER></BODY></HTML>"); } /** POST and GET requests handled identically. */ public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Example of Referer Request Header
jrun-powered.jpg,resin-powered.jpg and tomcat-power.gif images are required in folder c:/tomcat/webapps/ROOT/images/jrun-powered.jpg to run this example.
CustomizeImage.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that displays referer-specific image. */ public class CustomizeImage extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String referer = request.getHeader("Referer"); if (referer == null) { referer = "<I>none</I>"; } String title = "Referring page: " + referer; String imageName; if (contains(referer, "JRun")) { imageName = "jrun-powered.jpg"; } else if (contains(referer, "Resin")) { imageName = "resin-powered.jpg"; } else { imageName = "tomcat-power.gif"; } String imagePath = "../images/" + imageName; out.println( "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<CENTER><H2>" + title + "</H2>\n" + "<IMG SRC=\"" + imagePath + "\">\n" + "</CENTER></BODY></HTML>"); } private boolean contains(String mainString,String subString) { return(mainString.indexOf(subString) != -1); } }
Tomcat-Referer.html,JRun-Referer.html and Resin-Referer.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>Referer Test</TITLE></HEAD> <BODY BGCOLOR="#FDF5E6"> <H1 ALIGN="CENTER">Referer Test</H1> Click <A HREF="CustomizeImage">here</A> to visit the servlet. </BODY></HTML>
Example of MIME type
ApplesAndOranges.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ApplesAndOranges extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/vnd.ms-excel"); PrintWriter out = response.getWriter(); out.println("\tQ1\tQ2\tQ3\tQ4\tTotal"); out.println("Apples\t78\t87\t92\t29\t=SUM(B2:E2)"); out.println("Oranges\t77\t86\t93\t30\t=SUM(B3:E3)"); } }