This tutorial is based on http://www.ntu.edu.sg/home/ehchua/programming/java/JSPByExample.html
Contents
STEPS
0) Preparation.
1) Add new JSP file.
2) Edit the JSP file.
3) Run the JSP file.
4) Add a second JSP file.
5) Edit JSP File.
6) Run the JSP file.
DOWNLOAD
STEPS
0) Preparation.
You need to follow the tutorial at http://javadevsteps.blogspot.com/2014/06/myeclipse-create-new-dynamic-web-project.html or download the startup file MyFirstWebProject.zip
Open MyFirstWebProject.
1) Add new JSP file.
Right-Click WebRoot folder. Select New/Other…
Search “jsp” and select “JSP (Basic templates). Click Next.
Type a name “first.jsp”. Click Next.
Select “New JSP File(html)”. Click Finish.
“first.jsp” is added to the project.
2) Edit the JSP file.
Add the following codes to the <body> element. Save the file.
<%
double num = Math.random();
if (num > 0.95) {
%>
<h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
<%
}
%>
<a href="<%= request.getRequestURI() %>">Try Again</a>
|
(tips: double click the editor title window to enlarge the window for a more convenient way of editing codes)
|
Notice the following symbols in the example above:
- scriplet
- expression
3) Run the JSP file.
Right-Click file name, select Run As/Run Configurations…
Accept default settings. Click Run.
The server starts and finally the MyEclipse Web Browser displays the web page.
Since web site have got the default startup page (ie index.jsp), you need to append “first.jsp” to the URL.
This example demonstrates:
|
4) Add a second JSP file.
Type a name “echo.jsp”. Click Next.
“echo.jsp” file is added to the project.
5) Edit JSP File.
Add the following codes to the <body> element. Save the file.
<h3>Choose an author:</h3>
<form method="get">
<input type="checkbox" name="author" value="Tan Ah Teck">Tan
<input type="checkbox" name="author" value="Mohd Ali">Ali
<input type="checkbox" name="author" value="Kumar">Kumar
<input type="submit" value="Query">
</form>
<%
String[] authors = request.getParameterValues("author");
if (authors != null) {
%>
<h3>You have selected author(s):</h3>
<ul>
<%
for (int i = 0; i < authors.length; ++i) {
%>
<li><%= authors[i] %></li>
<%
}
%>
</ul>
<a href="<%= request.getRequestURI() %>">BACK</a>
<%
}
%>
|
This example demonstrates:
|
6) Run the JSP file.
Find the Run button in the Tool Bar.
Click the drop-down arrow and select “MyFirstWebProject”.
Browse “echo.jsp”. Click the checkbox for Tan. Click Query button.
Observe that:
|
No comments:
Post a Comment