Z-AJSP Codes

Image Viewer Using Jsp

 BACK

Image Viewer Using Jsp

web.xml

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ImageDownloadJsp</display-name>
<welcome-file-list>
<welcome-file>sample.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

[/xml]

sample.jsp

[java]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="ImageView.jsp">
<h2>click the button to view the image</h2>
<input type="submit" name="n1" value="image1"><br>
<input type="submit" name="n2" value="image2"><br>
<input type="submit" name="n3" value="image3"><br>
<input type="submit" name="n4" value="image4"><br>
<input type="submit" name="n5" value="image5"><br>
</form>
</body>
</html>

</form>
<pre>

[/java]

imageview.jsp

[java]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ page import="java.io.*"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.ServletOutputStream"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%

if(request.getParameter("n1") != null){
File f = new File(getServletContext().getRealPath("") + File.separator + "imag1.jpg");

FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
response.setContentType("image/jpeg");
int i = 0;
while (i != -1) {
i = fin.read();
outStream.write(i);
}
fin.close();
}
else if(request.getParameter("n2") != null)
{
File f = new File(getServletContext().getRealPath("") + File.separator + "imag2.jpeg");

FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
response.setContentType("image/jpeg");
int i = 0;
while (i != -1) {
i = fin.read();
outStream.write(i);
}
fin.close();
}

else if(request.getParameter("n3") != null)
{
File f = new File(getServletContext().getRealPath("") + File.separator + "imag3.jpeg");

FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
response.setContentType("image/jpeg");
int i = 0;
while (i != -1) {
i = fin.read();
outStream.write(i);
}
fin.close();
}
else if(request.getParameter("n4") != null)
{
File f = new File(getServletContext().getRealPath("") + File.separator + "imag4.jpeg");

FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
response.setContentType("image/jpeg");
int i = 0;
while (i != -1) {
i = fin.read();
outStream.write(i);
}
fin.close();
}
else if(request.getParameter("n5") != null)
{
File f = new File(getServletContext().getRealPath("") + File.separator + "imag5.jpeg");

FileInputStream fin = new FileInputStream(f);
ServletOutputStream outStream = response.getOutputStream();
response.setContentType("image/jpeg");
int i = 0;
while (i != -1) {
i = fin.read();
outStream.write(i);
}
fin.close();
}
%>
</body>
</html>
[/java]

Output Screenshots:

                                           download:sourcecode

BACK