JSP onload call servlet without scriptlets

14,426

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()">
Share:
14,426

Related videos on Youtube

Noah Martin
Author by

Noah Martin

Updated on June 04, 2022

Comments

  • Noah Martin
    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
    Noah Martin about 11 years
    I was thinking for a way without javascript, with EL for example, do you know smth like that?
  • Noah Martin
    Noah Martin about 11 years
    ok however as I see no one is responding here. I will vote up your answer, thank you anyway :)
  • azraelAT
    azraelAT about 11 years
    It's either JavaScript or using response.sendRedirect() in Scriptlet code. - But if I understood you correctly, scriptlets are off the table.