简体   繁体   中英

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. I have also written some validation code using javascript to verify the input.

Now after collecting the input I forward these two parameters to another page result.jsp using jQuery ajax as shown below:

$("#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. So, this result.jsp page contains all database logic(java code). But conventionally we should write all the business logic in a 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. If I use a servlet I need to use requestDispatcher which will forward to and hence load the entire result.jsp page!

I don't want to do that! I still want to load result.jsp page contents dynamically in the validate.jsp page. How to do that?

Or will there be any complications if I keep the entire database logic in result.jsp file?

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.

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.

Another way to do it is with the next AJAX request:

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

The above will only load the element with the class roll .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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