Tuesday, January 20, 2015

Creating a simple web application in netbeans to understand the use of jsp

Follow these steps to create a simple web application, here a simple web application is created step by step in order to get a sound knowledge  about the netbeans IDE and how jsp and web applications work.







1) Create a new project and select "java web" from the catagory and "web Application" from the projects catagory


2) Enter a project name and select a location for the project


3) In the next window select the server type that is used in the web application we are about to develop.


4) Here as we are developing a simple web application no framework has to be selected.Click "Finish" create the web application project.

Always HTML files and jsp files are added to the "web pages" package


To add a new jsp document right click on the "web pages" package and select "new" and then select "jsp".


Then enter a file name and click "finish" to create the jsp document.


Here we have created three Jsp documents to make you understand the use of jsp and how it work.


Home.jsp


formAction.jsp


Index.jsp



Index.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>creating a JSP application with netbeans</title>
    </head>
    <body>
        <h1>creating a JSP application with netbeans</h1>
        <%
double accounts[];//array named account with data type double
accounts = new double[200];//array size
accounts[4] = 5555.853;//assigning a value to array 4

out.println("Account 4 holds $" + accounts[4]);
%>
        
<%
%>
    </body>
</html>

home.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Codemonkeyy</title>
    </head>
    <body>
        <H1>Getting Parameter Names<H1>
<FORM ACTION="formAction.jsp" METHOD="POST"><pre> //open formaction.jsp with the post method
<b>Name</b>:<INPUT TYPE="TEXT" NAME="t1">
<SELECT NAME="select" SIZE="5" MULTIPLE>
<OPTION>1</OPTION>//drop
<OPTION selected>2</OPTION>
<OPTION> 3</OPTION>
<OPTION>4</OPTION>
<OPTION>5</OPTION>
</SELECT>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM></pre>
    </body>
</html>

formAction.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        
        <H1>Reading Parameter</H1>
Parameter Names:
<BR>
<% java.util.Enumeration names = request.getParameterNames();
while(names.hasMoreElements()){
out.println(names.nextElement() + "<BR>");
}
%>
        
    </body>
</html>






The above diagram shows how the jsp pages interact with each other, when the data is selected from the "home.jsp" page then it will be directed to the "formAction.jsp" page with the values from the "home.jsp" page.

home.jsp

formAction.jsp



Hope you got a sound knowledge  about web applications and the use of JSP in web applications. 

No comments:

Post a Comment