简体   繁体   English

通过提交选项更新数据库

[英]Update the database from submit option

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? 我有一个jsp页面,其中有一个``提交''选项供输入。单击它后,我想更新一个名为tbIndividual的表的值。什么是最好的方法?

On jsp page i have somthing like this : 在jsp页面上,我有这样的东西:

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 ? 使新的servlet完成此任务是一个不错的选择,还是在jsp中编写代码是更好的选择? If i need to make a new servlet then what will be the code for it on jsp page .?Please help. 如果我需要创建一个新的servlet,那么在jsp页面上的代码是什么。

First of all, there are certain things you need to understand while developing web applications in Java. 首先,在用Java开发Web应用程序时需要理解某些事情。 I have a few recommendations and inputs 我有一些建议和意见

  1. Please don't use Scriptlets . 请不要使用Scriptlets Scriptlets are deprecated and they make your code clumsy and the maintainance will be hard. 不推荐使用Scriptlet,它们会使您的代码笨拙且难以维护。 Use JSTL 使用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. 您需要在html创建一个form ,以具有一组变量,以在单击“ submit按钮时将其推送到服务器。 The form will have an action attribute which contains the URL where the request should be sent 该表单将具有一个action属性,其中包含应将请求发送到的URL
  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 创建一个Servlet并使用action URL映射,并编写doPost方法,该方法可以接收表单参数并执行业务逻辑,无论将状态从NO更改为WAIT还是进行其他操作
  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. 请注意,您需要具有Session变量才能在同一客户端的两次请求之间存储数据。 eg browser. 例如浏览器。

Is making new servlet for this task a good option or doing the code in jsp a better one ? 使新的servlet完成此任务是一个不错的选择,还是在jsp中编写代码是更好的选择?

Writing a new servlet is a good option, than doing the business logic in jsp page. 与在jsp页面中执行业务逻辑相比,编写新的servlet是一个不错的选择。 jsp is meant for presentation, just view. jsp仅用于演示,仅用于查看。 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 .? 如果我需要创建一个新的servlet,那么在jsp页面上的代码是什么?

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. JSP应该只具有所有必需的html元素和属性,例如表单,动作属性等。即可将请求中的参数发布到动作URL。 Please note the method attribute of form. 请注意表单的方法属性。 You should use HTTP POST method for posting form parameters to the server 您应该使用HTTP POST方法将表单参数发布到服务器

Note : Finally, Writing Servlets are also NOT recommended. 注:最后,编写servlet也并不推荐使用。 Instead, you should opt for webframeworks like Spring MVC , Struts etc. Please go through the link to understand about web frameworks answered by @BaluC 相反,您应该选择诸如Spring MVC ,Struts等之类的Webframeworks。请通过链接了解@BaluC回答的Web框架。

Hope this clarifies. 希望这可以澄清。

If you are trying to learn servlet with this project then you should create a separate servlet where you will perform your business logic (eg 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. 如果您尝试通过该项目学习servlet,则应创建一个单独的servlet,在其中执行业务逻辑(例如,在Db中添加元素),并且jsp应当远离业务逻辑,因为jsp的作用是呈现输出不产生输出。

If this is a project for production purposes, then you should ( IMO you must ) opt some web framework. 如果这是一个用于生产目的的项目,那么您(IMO必须)选择一些Web框架。 As framework will reduce your effort, headache and increase productivity. 由于框架将减少您的工作量,头痛并提高生产率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM