Saturday, May 24, 2014

Java EE 106 JSP Form and Post Method

---
Java EE 106 JSP Form and Post Method
This tutorial is based on http://www.youtube.com/watch?v=n30fkEtMkU4 
STEPS
1) Run Eclipse.
2) Open the project “ServletJSPExample”.
3) Create new JSP file.
4) Edit “index.jsp”
5) Edit “ServletExample.java”

STEPS

1) Run Eclipse.

2) Open the project “ServletJSPExample”.

3) Create new JSP file.

3.1) Right-click select New/Other…
3.2) Search for “JSP”.
3.3) Specify location and file name.
Accept default location.
Type name “index.jsp”
3.4) Select template.
New JSP File (xhtml).
Click Finish.
3.5) File “index.jsp” successfully created.

4) Edit “index.jsp”

4.1) Edit as follows:
(Add codes to <body> element)
        <form action="" method="post">
                <input type="text" name="firstname" />
                <input type="text" name="lastname" />
                <input type="submit" value="Submit" />
        </form>
4.2) Run On Server.
4.3) Outcome:
4.4) Add <table> element.
        <form action="" method="post">
                <table border="0">
                <tr>
                        <td></td><td><input type="text" name="firstname" /></td>
                </tr>
                <tr>
                        <td></td><td><input type="text" name="lastname" /></td>
                </tr>
                <tr>
                        <td colspan="2"><input type="submit" value="Submit" /></td>
                </tr>
                </table>
        </form>
4.5) Outcome:
4.6) Add labels.
4.7) Outcome:
4.8) Add “action” value.
4.9) Outcome:
(When you click the Submit button, you get the “Hello Java!” output (refer tutorial http://javadevsteps.blogspot.com/2014/05/java-ee-105-create-dynamic-web-project.html )

5) Edit the ServletExample.java page

5.1) Add codes to read the submitted data:
Replace “out.println("Hello Java!");” with the following:
                String firstName= request.getParameter("firstname");
                String lastName=request.getParameter("lastname");
                out.println(firstName + " " + lastName);
5.2) Outcome:
---

No comments:

Post a Comment