简体   繁体   English

结合使用jQuery ajax和servlet jsp

[英]using jQuery ajax with servlet jsp

I have a jsp form validate.jsp which contains 2 text fields where user enters his/her roll number/name. 我有一个jsp格式validate.jsp ,其中包含2个文本字段,用户可在其中输入他/她的卷号/名称。 I have also written some validation code using javascript to verify the input. 我还使用javascript编写了一些验证代码来验证输入。

Now after collecting the input I forward these two parameters to another page result.jsp using jQuery ajax as shown below: 现在,在收集输入之后,我将使用jQuery ajax将这两个参数转发到另一个页面result.jsp ,如下所示:

$("#student_result").load("result.jsp?"+ $.param( { rollNo: rollNo, name: name }));

Now my result.jsp page has to display result of the person whose roll no or name has been entered by the user. 现在,我的result.jsp页面必须显示用户输入了卷名或姓名的人员的结果。 So, this result.jsp page contains all database logic(java code). 因此,此result.jsp页面包含所有数据库逻辑(java代码)。 But conventionally we should write all the business logic in a servlet. 但是通常,我们应该在servlet中编写所有业务逻辑。 So I want to do this. 所以我想这样做。

But, as you can see, I am loading the contents of result.jsp dynamically using ajax inside the validate.jsp page. 但是,正如您所看到的,我正在validate.jsp页面内使用Ajax动态加载result.jsp的内容。 If I use a servlet I need to use requestDispatcher which will forward to and hence load the entire result.jsp page! 如果我使用servlet,则需要使用requestDispatcher,该请求将转发并加载整个result.jsp页面!

I don't want to do that! 我不想那样做! I still want to load result.jsp page contents dynamically in the validate.jsp page. 我仍然想在validate.jsp页面中动态加载result.jsp页面的内容。 How to do that? 怎么做?

Or will there be any complications if I keep the entire database logic in result.jsp file? 如果将整个数据库逻辑都保存在result.jsp文件中,会不会有麻烦?

I think there are better approaches to solve your problem. 我认为有更好的方法来解决您的问题。 If you don't want to use a framework I think you should use servlets at least. 如果您不想使用框架,我认为您至少应该使用servlet。

If you still want to do it using only jsps your result.jsp should only return the fragment you want to load in you page, and not a whole html page. 如果您仍然只想使用jsps进行操作,那么result.jsp应该只返回您要在页面中加载的片段,而不是整个html页面。

Another way to do it is with the next AJAX request: 另一种方法是使用下一个AJAX请求:

$("#student_result").load("result.jsp .roll", {rollNo: rollNo, name: name });

The above will only load the element with the class roll . 上面只会加载class roll元素。

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

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