JSP Scriptlet Tag
JSP Scriptlet Tag
Scripting elements
The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements:
index.html
<html> <body> <form action="welcome.jsp"> <input type="text" name="uname">
SHARAN K P
Page 1
JSP DOCUMENTS
<input type="submit" value="go"><br/> </form> </body> </html>
welcome.jsp
<html> <body> <% String name=request.getParameter("uname"); out.print("welcome "+name); %> </form> </body> </html> next>>
Note: Do not end your statement with semicolon in case of expression tag.
SHARAN K P
Page 2
JSP DOCUMENTS
instance of Calendar class by the getInstance() method.
index.jsp
<html> <body> Current Time: <%= java.util.Calendar.getInstance().getTime() %> </body> </html>
index.html
<html> <body> <form action="welcome.jsp"> <input type="text" name="uname"><br/> <input type="submit" value="go"> </form> </body> </html>
welcome.jsp
<html> <body> <%= "Welcome "+request.getParameter("uname") %> </form> </body> </html>
SHARAN K P
Page 3
JSP DOCUMENTS
get memory at each request.
What is the difference between the jsp scriptlet tag and jsp declaration tag ?
The jsp scriptlet tag can only declare variables not methods whereas jsp declaration tag can declare variables as well as methods. The declaration of scriptlet tag is placed inside the _jspService() method whereas the declaration of jsp declaration tag is placed outside the _jspService() method.
index.jsp
<html> <body> <%! int data=50; %> <%= "Value of the variable is:"+data %> </body> </html>
index.jsp
<html> <body> <%!
SHARAN K P
Page 4
JSP DOCUMENTS
int cube(int n){ return n*n*n*; } %> <%= "Cube of 3 is:"+cube(3) %> </body> </html>
Object
out request response config application session pageContext page exception JspWriter
Type
JSP DOCUMENTS
In this example we are simply displaying date and time.
index.jsp
<html> <body> <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> </body> </html>
welcome.jsp
<html> <body> <% String name=request.getParameter("uname"); out.print("welcome "+name); %> </body> </html> next>>
SHARAN K P
Page 6
JSP DOCUMENTS
<form action="welcome.jsp"> <input type="text" name="uname"> <input type="submit" value="go"><br/> </form> </body> </html> welcome.jsp <html> <body> <% response.sendRedirect("http://www.google.com"); %> </body> </html> next>>
SHARAN K P
Page 7
JSP DOCUMENTS
</servlet-mapping> </web-app> welcome.jsp <html> <body> <% out.print("Welcome "+request.getParameter("uname")); String driver=config.getInitParameter("dname"); out.print("driver name is="+driver); %> </body> </html> next>>
SHARAN K P
Page 8
JSP DOCUMENTS
</web-app> welcome.jsp <html> <body> <% out.print("Welcome "+request.getParameter("uname")); String driver=application.getInitParameter("dname"); out.print("driver name is="+driver); %> </body> </html> next>>
welcome.jsp
<html> <body> <% String name=request.getParameter("uname"); out.print("Welcome "+name); session.setAttribute("user",name); <a href="second.jsp">second jsp page</a> %> </body> </html>
SHARAN K P
Page 9
JSP DOCUMENTS
second.jsp
<html> <body> <% String name=(String)session.getAttribute("user"); out.print("Hello "+name); %> </body> </html>
welcome.jsp
<html> <body> <% String name=request.getParameter("uname"); out.print("Welcome "+name); pageContext.setAttribute("user",name,PageContext.SESSION_SCOPE);
SHARAN K P
Page 10
JSP DOCUMENTS
<a href="second.jsp">second jsp page</a> %> </body> </html>
second.jsp
<html> <body> <% String name=(String)pageContext.getAttribute("user",PageContext.SESSION_SCOPE); out.print("Hello "+name); %> </body> </html> next>>
SHARAN K P
Page 11
JSP DOCUMENTS
</html> To get the full example, click here full example of exception handling in jsp. But, it will be better to learn it after the JSP Directives next>>
JSP directives
1. JSP directives 1. page directive 2. Attributes of page directive 1. import 2. contentType 3. extends 4. info 5. buffer 6. language 7. isELIgnored 8. isThreadSafe 9. errorPage 10. isErrorPage The directives are messages that tells the web container how to translate a JSP page into corresponding servlet.There are three types of directives:
JSP DOCUMENTS
import contentType extends info buffer language isELIgnored isThreadSafe autoFlush session pageEncoding errorPage isErrorPage
1)import
The import attribute is used to import class,interface or all the members of a package.It is similar to import keyword in java class or interface.
2)contentType
The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the HTTP response.The default value is "text/html;charset=ISO-8859-1".
SHARAN K P
Page 13
JSP DOCUMENTS
</html>
3)extends
The extends attribute defines the parent class that will be inherited by the generated servlet.It is rarely used.
4)info
This attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP page.The default size of the buffer is 8Kb.
SHARAN K P
Page 14
JSP DOCUMENTS
6)language
The language attribute specifies the scripting language used in the JSP page. The default value is "java".
7)isELIgnored
We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default its value is false i.e. Expression Language is enabled by default. We see Expression Language later. <%@ page isELIgnored="true" %>//Now EL will be ignored
8)isThreadSafe
Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP page, you can use isThreadSafe attribute of page directive.The value of isThreadSafe value is true.If you make it false, the web container will serialize the multiple requests, i.e. it will wait until the JSP finishes responding to a request before passing another request to it.If you make the value of isThreadSafe attribute like: <%@ page isThreadSafe="false" %> The web container in such a case, will generate the servlet as: public class SimplePage_jsp extends HttpJspBase implements SingleThreadModel{ ....... }
9)errorPage
The errorPage attribute is used to define the error page, if exception occurs in the current page, it will be redirected to the error page.
SHARAN K P
Page 15
JSP DOCUMENTS
</body> </html>
10)isErrorPage
The isErrorPage attribute is used to declare that the current page is the error page.
Note: The exception object can only be used in the error page.
Include directive
1. Include directive 2. Advantage of Include directive 3. Example of include directive The include directive is used to include the contents of any resource it may be jsp file, html file or text file. The include directive includes the original content of the included resource at page translation time (the jsp page is translated only once so it will be better to include static resource).
JSP DOCUMENTS
In this example, we are including the content of the header.html file. To run this example you must create an header.html file. 1. <html> <body> <%@ include file="header.html" %> Today is: <%= java.util.Calendar.getInstance().getTime() %> </body> </html>
Note: The include directive includes the original content, so the actual page size grows at runtime.
SHARAN K P
Page 17
JSP DOCUMENTS
SHARAN K P
Page 18
JSP DOCUMENTS
Example of exception handling in jsp by the elements of page directive
In this case, you must define and create a page to handle the exceptions, as in the error.jsp page. The pages where may occur exception, define the errorPage attribute of page directive, as in the process.jsp page. There are 3 files:
index.jsp for input values process.jsp for dividing the two numbers and displaying the result error.jsp for handling the exception
index.jsp
<form action="process.jsp"> No1:<input type="text" name="n1" /><br/><br/> No1:<input type="text" name="n2" /><br/><br/> <input type="submit" value="divide"/> </form>
process.jsp
<%@ page errorPage="error.jsp" %> <% String num1=request.getParameter("n1"); String num2=request.getParameter("n2"); int a=Integer.parseInt(num1); int b=Integer.parseInt(num2); int c=a/b; out.print("division of numbers is: "+c); %>
error.jsp
<%@ page isErrorPage="true" %> <h3>Sorry an exception occured!</h3> Exception is: <%= exception %>
SHARAN K P
Page 19
JSP DOCUMENTS
SHARAN K P
Page 20
JSP DOCUMENTS
Example of exception handling in jsp by specifying the error-page element in web.xml file
This approach is better because you don't need to specify the errorPage attribute in each jsp
SHARAN K P
Page 21
JSP DOCUMENTS
page. Specifying the single entry in the web.xml file will handle the exception. In this case, either specify exception-type or error-code with the location element. If you want to handle all the exception, you will have to specify the java.lang.Exception in the exception-type element. Let's see the simple example: There are 4 files:
web.xml file for specifying the error-page element index.jsp for input values process.jsp for dividing the two numbers and displaying the result error.jsp for displaying the exception
1) web.xml file if you want to handle the exception for a specific error code
<web-app> <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page> </web-app>
SHARAN K P
Page 22
JSP DOCUMENTS
4) error.jsp file is same as in the above example
The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean development. So we will see these tags in bean developement.
index.jsp
SHARAN K P
Page 23
JSP DOCUMENTS
<html> <body> <h2>this is index page</h2> <jsp:forward page="printdate.jsp" /> </body> </html>
printdate.jsp
<html> <body> <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> </body> </html>
index.jsp
<html> <body> <h2>this is index page</h2> <jsp:forward page="printdate.jsp" > <jsp:param name="name" value="javatpoint.com" /> </jsp:forward> </body> </html>
printdate.jsp
<html> <body> <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> <%= request.getParameter("name") %> </body> </html> next>>
SHARAN K P
Page 24
JSP DOCUMENTS
3. Example of jsp:include action tag without parameter The jsp:forward action tag is used to include the content of another resource it may be jsp, html or servlet. The jsp include action tag includes the resource at request time so it is better for dynamic pages because there might be changes in future.
index.jsp
<html> <body> <h2>this is index page</h2> <jsp:include page="printdate.jsp" /> <h2>end section of index page</h2> </body> </html>
printdate.jsp
<html> <body> <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> </body> </html> next>>
Java Bean
SHARAN K P Page 25
JSP DOCUMENTS
A Java Bean is a java class that should follow following conventions:
It should have a no-arg constructor. It should be Serializable. It should provide methods to set and get the values of the properties, known as getter and setter methods.
Note: There are two ways to provide values to the object, one way is by constructor and second is by setter method.
SHARAN K P
Page 26
JSP DOCUMENTS
SHARAN K P
Page 27
JSP DOCUMENTS
Calculator.java (a simple Bean class)
package com.javatpoint; public class Calculator{ public int cube(int n){return n*n*n;} }
index.jsp file
<jsp:useBean id="obj" class="com.javatpoint.Calculator"/> <% int m=obj.cube(5); out.print("cube of 5 is "+m); %>
SHARAN K P
Page 28
JSP DOCUMENTS
component that represents data. The jsp:setProperty action tag sets a property value or values in a bean using the setter method.
Example of jsp:setProperty action tag if you have to set all the values of incoming request in the bean
<jsp:setProperty name="bean" property="*" />
Example of jsp:setProperty action tag if you have to set value of the incoming specific property
<jsp:setProperty name="bean" property="username" />
Example of jsp:setProperty action tag if you have to set a specific value in the property
<jsp:setProperty name="bean" property="username" value="Kumar" />
JSP DOCUMENTS
In this example there are 3 pages:
index.html for input of values welocme.jsp file that sets the incoming values to the bean object and prints the one value User.java bean class that have setter and getter methods
index.html
<form action="welcome.jsp"/> Enter Name:<input type="text" name="name"/> </form>
welcome.jsp
<jsp:useBean id="obj" class="com.javatpoint.User" /> <jsp:setProperty name="obj" property="*" /> Welcome, <jsp:getProperty name="obj" property="name" />
User.java
package com.javatpoint; public class User{ private String name; public void setName(String name){ this.name=name; } public String getName(){ return name; } }
SHARAN K P
Page 30
JSP DOCUMENTS
index.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Mouse Drag</title> </head> <body bgcolor="khaki"> <h1>Mouse Drag Example</h1>
SHARAN K P
Page 31
JSP DOCUMENTS
<jsp:plugin align="middle" height="500" width="500" type="applet" code="MouseDrag.class" name="clock" codebase="."/> </body> </html>
Implicit Objects
pageScope
Usage
it maps the given attribute name with the value set in the page scope it maps the given attribute name with the value set in the request scope
requestScope
sessionScope
it maps the given attribute name with the value set in the session scope
SHARAN K P
Page 32
JSP DOCUMENTS
applicationScope it maps the given attribute name with the value set in the application scope it maps the request parameter to the single value it maps the request parameter to an array of values it maps the request header name to the single value it maps the request header name to an array of values it maps the given cookie name to the cookie value it maps the initialization parameter it 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
<form action="process.jsp"> Enter Name:<input type="text" name="name" /><br/><br/> <input type="submit" value="go"/> </form>
process.jsp
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
SHARAN K P Page 33
JSP DOCUMENTS
<h3>welcome to index page</h3> <% session.setAttribute("user","sonoo"); %> <a href="process.jsp">visit</a>
process.jsp
Value is ${ sessionScope.user }
Precedence of Operators in EL
There are many operators that have been provided in the Expression Language. Their precedence are as follows: [] .
()
* / div % mod
+ - (binary)
== != eq ne
&& and
|| or
?:
Reserve words in EL
There are many reserve words in the Expression Language. They are as follows:
SHARAN K P
Page 34
JSP DOCUMENTS
lt eq and le ne or gt true not ge false instanceof
div
mod
empty
null
MVC in JSP
1. MVC in JSP 2. Example of following MVC in JSP MVC stands for Model View and Controller. It is a design pattern that separates the business logic, presentation logic and data. Controller acts as an interface between View and Model. Controller intercepts all the requests. Modelrepresents the state of the application i.e. data. Viewrepresents the presentaion.
MServlet.java a servlet that acts as a controller. login.jsp and welcome.jsp files acts as view components. web.xml file for mapping the servlet.
MServlet.java In this servlet, we are simply checking the input values, if run this application first time, it will for the request to the login page without error message. If you type any other password than sonoo, it will redirect again to the login page with error message. But if you type the password sonoo, it will redirect to the welcome page. import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
SHARAN K P
Page 35
JSP DOCUMENTS
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n=request.getParameter("name"); String p=request.getParameter("pass"); if(n==null){ RequestDispatcher rd=request.getRequestDispatcher("login.jsp"); rd.forward(request, response); } else if(n.equals("sonoo")){ RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp"); rd.forward(request, response); } else{ request.setAttribute("error","true"); RequestDispatcher rd=request.getRequestDispatcher("login.jsp"); rd.forward(request, response); } out.close(); } } login.jsp <% if(request.getAttribute("error")!=null){ out.print("Not valid user! Try again<hr>"); } %> <form action="servletM"> Name:<input type="text" name="name"/><br/> Password:<input type="password" name="pass"/><br/> <input type="submit" value="login"/> </form> welcome.jsp Welcome User! web.xml <web-app>
SHARAN K P
Page 36
JSP DOCUMENTS
<servlet> <servlet-name>MServlet</servlet-name> <servlet-class>MServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MServlet</servlet-name> <url-pattern>/servletM</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>servletM</welcome-file> </welcome-file-list> </web-app>
next>>
index.jsp for getting the values from the user User.java, a bean class that have properties and setter and getter methods. process.jsp, a jsp file that processes the request and calls the methods Provider.java, an interface that contains many constants like DRIVER_CLASS, CONNECTION_URL, USERNAME and PASSWORD ConnectionProvider.java, a class that returns an object of Connection. It uses the Singleton and factory method design pattern. RegisterDao.java, a DAO class that is responsible to get access to the database
SHARAN K P
Page 37
JSP DOCUMENTS
index.jsp
We are having only three fields here, to make the concept clear and simplify the flow of the application. You can have other fields also like country, hobby etc. according to your requirement. <form action="process.jsp"> <input type="text" name="uname" value="Name..." onclick="this.value=''"/><br/> <input type="text" name="uemail" value="Email ID..." onclick="this.value=''"/><br/> <input type="password" name="upass" value="Password..." onclick="this.value=''"/><br/> <input type="submit" value="register"/> </form>
process.jsp
This jsp file contains all the incoming values to an object of bean class which is passed as an argument in the register method of the RegisterDao class. <%@page import="bean.RegisterDao"%> <jsp:useBean id="obj" class="bean.User"/> <jsp:setProperty property="*" name="obj"/> <% int status=RegisterDao.register(obj); if(status>0) out.print("You are successfully registered"); %>
User.java
It is the bean class that have 3 properties uname, uemail and upass with its setter and getter methods. package bean; public class User { private String uname,upass,uemail; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } public String getUemail() {
SHARAN K P
Page 38
JSP DOCUMENTS
return uemail; } public void setUemail(String uemail) { this.uemail = uemail; } }
Provider.java
This interface contains four constants that can vary from database to database. package bean; public interface Provider { String DRIVER="oracle.jdbc.driver.OracleDriver"; String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe"; String USERNAME="system"; String PASSWORD="oracle"; }
ConnectionProvider.java
This class is responsible to return the object of Connection. Here, driver class is loaded only once and connection object gets memory only once. package bean; import java.sql.*; import static bean.Provider.*; public class ConnectionProvider { private static Connection con=null; static{ try{ Class.forName(DRIVER); con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD); }catch(Exception e){} } public static Connection getCon(){ return con; } }
RegisterDao.java
This class inserts the values of the bean component into the database. package bean; import java.sql.*; public class RegisterDao {
SHARAN K P
Page 39
JSP DOCUMENTS
public static int register(User u){ int status=0; try{ Connection con=ConnectionProvider.getCon(); PreparedStatement ps=con.prepareStatement("insert into user432 values(?,?,?)"); ps.setString(1,u.getUname()); ps.setString(2,u.getUemail()); ps.setString(3,u.getUpass()); status=ps.executeUpdate(); }catch(Exception e){} return status; } }
index.jsp it provides three links for login, logout and profile login.jsp for getting the values from the user loginprocess.jsp, a jsp file that processes the request and calls the methods. LoginBean.java, a bean class that have properties and setter and getter methods. Provider.java, an interface that contains many constants like DRIVER_CLASS, CONNECTION_URL, USERNAME and PASSWORD ConnectionProvider.java, a class that is responsible to return the object of Connection. It uses the Singleton and factory method design pattern. LoginDao.java, a DAO class that verifies the emailId and password from the database. logout.jsp it invalidates the session. profile.jsp it provides simple message if user is logged in, otherwise forwards the request to the login.jsp page.
In this example, we are using the Oracle10g database to match the emailId and password with the database. The table name is user432 which have many fields like name, email, pass etc. You may use this query to create the table: CREATE TABLE "USER432" ( "NAME" VARCHAR2(4000), "EMAIL" VARCHAR2(4000),
SHARAN K P
Page 40
JSP DOCUMENTS
"PASS" VARCHAR2(4000) ) / We assume that there are many records in this table.
index.jsp
It simply provides three links for login, logout and profile. <a name="jsploginex"></a><a href="login.jsp">login</a>| <a href="logout.jsp">logout</a>| <a href="profile.jsp">profile</a>
login.jsp
This file creates a login form for two input fields name and password. It is the simple login form, you can change it for better look and feel. We are focusing on the concept only. <%@ include file="index.jsp" %> <hr> <h3>Login Form</h3> <% String profile_msg=(String)request.getAttribute("profile_msg"); if(profile_msg!=null){ out.print(profile_msg); } String login_msg=(String)request.getAttribute("login_msg"); if(login_msg!=null){ out.print(login_msg); } %> <br> <form action="loginprocess.jsp" method="post"> Name:<input type="text" name="name"><br><br> Password:<input type="password" name="password"><br><br> <input type="submit" value="login">" </form>
loginprocess.jsp
This jsp file contains all the incoming values to an object of bean class which is passed as an argument in the validate method of the LoginDao class. If emailid and password is correct, it displays a message you are successfully logged in! and maintains the session so that we may recognize the user. <%@page import="bean.LoginDao"%> <jsp:useBean id="obj" class="bean.LoginBean"/> <jsp:setProperty property="*" name="obj"/> <% boolean status=LoginDao.validate(obj); if(status){ out.println("You r successfully logged in");
SHARAN K P
Page 41
JSP DOCUMENTS
session.setAttribute("session","TRUE"); } else { out.print("Sorry, email or password error"); %> <jsp:include page="index.jsp"></jsp:include> <% } %>
LoginBean.java
It is the bean class that have 2 properties email and pass with its setter and getter methods. package bean; public class LoginBean { private String email,pass; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } }
Provider.java
This interface contains four constants that may differ from database to database. package bean; public interface Provider { String DRIVER="oracle.jdbc.driver.OracleDriver"; String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe"; String USERNAME="system"; String PASSWORD="oracle"; }
ConnectionProvider.java
This class provides a factory method that returns the object of Connection. Here, driver class is loaded only once and connection object gets memory only once because it is static.
SHARAN K P
Page 42
JSP DOCUMENTS
package bean; import java.sql.*; import static bean.Provider.*; public class ConnectionProvider { private static Connection con=null; static{ try{ Class.forName(DRIVER); con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD); }catch(Exception e){} } public static Connection getCon(){ return con; } }
LoginDao.java
This class varifies the emailid and password. package bean; import java.sql.*; public class LoginDao { public static boolean validate(LoginBean bean){ boolean status=false; try{ Connection con=ConnectionProvider.getCon(); PreparedStatement ps=con.prepareStatement( "select * from user432 where email=? and pass=?"); ps.setString(1,bean.getEmail()); ps.setString(2, bean.getPass()); ResultSet rs=ps.executeQuery(); status=rs.next(); }catch(Exception e){} return status; } }
SHARAN K P
Page 43
JSP DOCUMENTS
2. MultipartRequest class 3. Constructors of MultipartRequest class 4. Example of File Upload in JSP There are many ways to upload the file to the server. One of the way is by the MultipartRequest class. For using this class you need to have the cos.jar file. In this example, we are providing the cos.jar file alongwith the code.
MultipartRequest class
It is a utility class to handle the multipart/form-data request. There are many constructors defined in the MultipartRequest class.
index.jsp
To upload the file to the server, there are two requirements: 1. You must use the post request. 2. encodeType should be multipart/form-data that gives information to the server that you are going to upload the file. <form action="upload.jsp" method="post" enctype="multipart/form-data"> Select File:<input type="file" name="fname"/><br/> <input type="image" src="MainUpload.png"/> </form>
upload.jsp
We are uploading the incoming file to the location d:/new, you can specify your location here. <%@ page import="com.oreilly.servlet.MultipartRequest" %> <% MultipartRequest m = new MultipartRequest(request, "d:/new");
SHARAN K P
Page 44
JSP DOCUMENTS
out.print("successfully uploaded"); %> If size of the file is greater than 1MB, you should specify the post size.
index.jsp
This file provides a link to download the jsp file. <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. <% String filename = "home.jsp"; String filepath = "e:\\"; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename); int i; while ((i=fileInputStream.read()) != -1) { out.write(i); } fileInputStream.close(); %>
SHARAN K P
Page 45