SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Simple JSP Example

 Introduction

JSP files contain HTML tags along with embedded code that allows the page to access data from Java code running on the server. When a page is requested by a user the HTML portion of the page is only viewed. The code portions of the pages are executed at the time the request is received, and the dynamic content generated by this code is spliced into the page before it is sent to the user. This separate the HTML from the programming logic contained in the code

CLICK HERE to download this complete example (zip file)


 Steps to Create a JSP Application (Simple Example)

A JSP application may consist of HTML, JavaBean, or JSP files. You need to follow the following steps to create a JSP application:

  • Create a user interface
  • Create a JSP page
  • Package the user interface and JSP page:
  • Deploy the package you have created

Access the application using a Web browser:

 HTML file (main.html)

<html>
<title>Sample Example </title>
<body>
<h1> <center> Example of JSP </center> </h1>
<b> Mathematics</b>
<hr>
<form method="post" action="a.jsp">
<font size=5 face="Times New Roman">
<input type="radio" name="a1" value="add" checked>Addition</input><br>
<input type="radio" name="a1" value="mul" >Multiplication</input><br>
<input type="radio" name="a1" value="div" >Division</input><br>
</font>
<br><br>
Enter first Value      <input type="text" name="t1" value=""><br>
Enter second Value  <input type="text" name="t2" value=""><br>
<input type="submit" name="result">
</form>
</body>
</html>
Download: main.html

 JSP file (a.jsp)

<%@ page language="java"%>
<%@ page import="java.lang.*"%>
<html>
<body>
<H1><center>Result for <%=request.getParameter("a1")%></center></H1>
<%
int i=Integer.parseInt(request.getParameter("t1"));
int j=Integer.parseInt(request.getParameter("t2"));
int k=0;
String str=request.getParameter("a1");
if(str.equals("add"))
k=i+j;
if(str.equals("mul"))
k=i*j;
if(str.equals("div"))
k=i/j;
%>
Result is <%=k%>
</body>
</html>
Download: a.jsp

 Steps to deploy and run this JSP Project

  1. Write a html file and name it as main.html
  2. Write a jsp file and name it as a.jsp
  3. Now goto Start -> Programs -> Sun Microsystems ->Application Server PE -> Start Default Server (wait till it start and then press any key). CLICK HERE to see how to Start the Server
  4. Next goto Start -> Programs -> Sun Microsystems ->Application Server PE -> Deploytool. CLICK HERE to see how to Start the Deploytool
    Now in the deploytool,
    1. Goto File ->New -> Application

      (Click the Browse button)


    2. (Select the folder in the Look In dropdown box, and then give a file name "Calculation". Next click the New Application button)


    3. (Click the OK button)
    4. Now goto File -> Save to save the file
    5. Next, goto File -> New -> Web Component

      (Click Next button)


    6. (Enter the WAR Name as "CalApp" and then click the Edit Contents… button)


    7. (Select all the .class, .jsp , .tld and .html files and click the Add button)


    8. (Now click the OK button)


    9. (Now click the Next button)


    10. (Now select the JSP Page option button and then click the Next button)


    11. (Now select the "a.jsp" from the Servlet Class dropdown box)


    12. (Now select the Next button)


    13. (Now select the Finish button)


    14. (Now select the CalApp in the left pane and select the General tab in the right pane. Here give a name calculation in the Context Root text box)
    15. Note: Here, no need Aliases name because the client HTML file directly calls the JSP file
    16. Now goto File ->Save
    17. Next goto Tools -> Deployee

      (Enter the User Name as “admin” and Password as “password” (CLICK HERE for password). Next click the OK button)


    18. (Now a message --- Operation Completed Successfully --- must display. Next click the Close button)
    19. Next goto File -> Exit to close it

 Click Next To Continue ...
<- PREVIOUSNEXT ->