Friday, June 6, 2014

MyEclipse - Creating Basic JSP Files

---
MyEclipse: Creating Basic JSP Files
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.

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:
  • Data/DataType: double num
  • Data operator: num > 0.95
  • Object/Method: Math.random
  • Object/Method: request.getRequestURI()

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:
  • String data: String[] authors
  • Object/Method: request.getParameterValues
  • String Object Method: authors.length
  • Object index values: authors[i]

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:
  • 1. The URL is appended with “?author=Tan+Ah+Teck”
  • 2. The page displays additional information.

DOWNLOAD

---

No comments:

Post a Comment