1

I have a jsp page having a 'submit' option for input.On clicking it i want to update the value of a table called tbIndividual.What can be the best approach to do it?

On jsp page i have somthing like this :

User Name : <%=rs.getString(2)%>
First Name : <%=rs.getString(4)%> 
Last Name : <%=rs.getString(5)%>
Email Id : <%=rs.getString(6)%>
Contact : <%=rs.getString(7)%>
<input type="submit" value="ADD"></input>

And now i want to update the value of status of that particular individual from 'NO' to 'WAIT' state.On click of this submit button.

Is making new servlet for this task a good option or doing the code in jsp a better one ? If i need to make a new servlet then what will be the code for it on jsp page .?Please help.

user3462609
  • 105
  • 2
  • 11

2 Answers2

0

First of all, there are certain things you need to understand while developing web applications in Java. I have a few recommendations and inputs

  1. Please don't use Scriptlets. Scriptlets are deprecated and they make your code clumsy and the maintainance will be hard. Use JSTL
  2. You need to create a form in your html to have a set of variables to push them to the server on clicking submit button. The form will have an action attribute which contains the URL where the request should be sent
  3. Create a Servlet and map with the action URL and write the doPost method which can receive the form parameters and do the business logic whatever changing the status from NO to WAIT or anything else
  4. Please take a note that you need to have Session variables in order to have the store the data in between requests from the same client. eg browser.

Is making new servlet for this task a good option or doing the code in jsp a better one ?

Writing a new servlet is a good option, than doing the business logic in jsp page. jsp is meant for presentation, just view. They shouldn't be used to perform business logic

If i need to make a new servlet then what will be the code for it on jsp page .?

JSP should just have all the necessary html elements and attributes like form, action attribute etc.. to post the parameters in the request to the action URL. Please note the method attribute of form. You should use HTTP POST method for posting form parameters to the server

Note : Finally, Writing Servlets are also NOT recommended. Instead, you should opt for webframeworks like Spring MVC , Struts etc. Please go through the link to understand about web frameworks answered by @BaluC

Hope this clarifies.

Community
  • 1
  • 1
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • As long as you're describing best practices, hand-writing servlets is a Bad Idea. A Web framework is a much more reliable and easier option. – chrylis -cautiouslyoptimistic- Mar 28 '14 at 04:15
  • Since the OP question is related to `Servlets`, I just recommended the best ways of writing it. Anyway it would be good to learn servlets first and then move on to web frameworks.i will add the frameworks should be recommended – Keerthivasan Mar 28 '14 at 04:18
0

If you are trying to learn servlet with this project then you should create a separate servlet where you will perform your business logic (e.g addition of element in Db) and jsp should be kept away from business logic because role of jsp is to render the output not to produce the output.

If this is a project for production purposes, then you should ( IMO you must ) opt some web framework. As framework will reduce your effort, headache and increase productivity.

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72