JSP onload call servlet without scriptlets
Solution 1
Although its marked as resolved but I am editing my answer for future reference: Apart from the javascript solution you can accomplish this with 2 more options using jsp tags:
Option1: You can forward the request to the corresponding servlet. Use jsp standard action jsp:forward, e.g.:
<jsp:forward page="ContentServlet?action=userContents" >
</jsp:forward>
You can replace your link with the above tag and the servlet will be called.
Option2: you can redirect the request to your servlet using JSTL tags:
<c:redirect url="ContentServlet?action=userContents" />
Again you can replace your link with the above tag.
In Option1 browser's url will not change. In Option 2 browser's url will change to "ContentServlet?action=userContents"
Hope it solves your problem.
Solution 2
Why don't you use JavaScript?
<script type="text/javascript">
function redirect(){
window.location = "/ContentServlet?action=userContents"
}
</script>
...
<body onLoad="redirect()">
Related videos on Youtube
Noah Martin
Updated on June 04, 2022Comments
-
Noah Martin almost 2 years
I want that when my JSP is loaded to call a servlet. I have this link:
<a href="ContentServlet?action=userContents">Homepage</a>
But this way I have to click the link, I want to perform the calling automatically when the jsp is loaded.
On the other side I need to use no scriptlets. Does anyone have any idea how to do this?
-
Noah Martin about 11 yearsI was thinking for a way without javascript, with EL for example, do you know smth like that?
-
Noah Martin about 11 yearsok however as I see no one is responding here. I will vote up your answer, thank you anyway :)
-
azraelAT about 11 yearsIt's either JavaScript or using response.sendRedirect() in Scriptlet code. - But if I understood you correctly, scriptlets are off the table.