Engineering Full Stack Apps with Java and JavaScript
We will create a simple JSP form and then submit the form with action as another jsp and retrieve the form parameters which are received as request parameters in the second jsp file.
Create a 'Dynamic Web Project'.
Select 'File > New > Dynamic Web Project' or 'File > New > Project > Web > Dynamic Web Project'.
Give a name for the project, say 'MyDynaProject' in the wizrard, leave everything else as defaults and click Finish.
Create a jsp file inside WebContent folder.
Right click on the WebContent folder and select 'New > JSP File' or 'New > Other > Web > JSP File', give the name first.jsp and click finish.
Open the JSP file and within the body tags (<body> and </body>), place the below code:
<FORM METHOD=POST ACTION="second.jsp">
Please enter your name: <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
<P><INPUT TYPE=SUBMIT>
</FORM>
This will create a form that will ask user for his name. Once the data is submitted, the jsp/servlet given in the ACTION (e.g. second.jsp) is invoked passing the form parameters (e.g. username) as request parameters.
Now create another JSP file the same way inside WebContent and give the name 'second.jsp'
Hello <%= request.getParameter("username")%>.
Now execute it on server:
Right click first.jsp and select Run As > Run on Server
Select Tomcat (Make sure status is started) and click Next
Make sure your project is there on the right side under 'Configured'. If project is not under 'Configured, but under 'Available', then select the project under 'Available', click 'Add' and move it to 'Configured' and click Next. If your project is not under both 'Configured' and 'Available', then something has gone wrong. Check if you have followed all the steps correctly.
Finally clck finish and your jsp file will be loaded in the browser.
Enter your name in the first.jsp file loaded and click submit.
You will now see second.jsp with Hello <Name entered>
Export the project as a war, deploy it in a standalone sandbox and restart tomcat.
Execute the form to verify the output
See the generated servlet source code and compare with your source.
Create a bean class that can hold the values of the form submission
Instantiate the bean, set properties from the form submission parameters and print the property values, all using only standard actions