Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JSP page extend abstract HttpServlet

I'm working on a Web application that works with multiple tables in a database. The design requirements specify that each table have its own page to work with. For the most part, a good chunk of the server-side code is the same across all the pages, with the page specific stuff mostly dealing with the sql for the specific tables (and they all have different structures, BTW).

So I thought it would be prudent to organize the code so that all the common methods were all in one shared place and leaving only the unique implementations on the JSP pages.

The structure I have attempted to create is to have an abstract superclass, which we will call MyServlet, that extends HttpServlet. Each of my JSP pages extend this superclass.

However when I went to test this out my pages now fail to load with HTTP 405 Method not allowed error.

For security/business reasons I cannot simply copy and paste my code here, but I can post a general structure. My Servlet class looks something like this:

public abstract class MyServlet extends HttpServlet
{
     public void someMethod()
     {
          abstractMethod();
     }

     protected abstract void abstractMethod();
}

And my jsp like this:

<%@ extends="package.MyServlet" %>
<%! 
     public void abstractMethod()
     {
          someCode;
     }
%>
<% someMethod() %>

I should point out that MyServlet does not implement doGet() or doPost(). I was under the assumption that with JSP pages themselves being extensions of HttpServlet that they provided implementations for those methods, though I am also aware that error 405 happens because at least doGet() is not there.

Is the error happening because I have not implemented doGet(), and if so how do I implement it such that it works with my JSP pages? Or is there some other reason why I am getting this error?

like image 709
user2707633 Avatar asked Feb 10 '26 07:02

user2707633


1 Answers

can you just create your method in some utility class and then include it, instead of extending it?

<%@ page import="org.foo.HelperClass, org.foo.StaticHelper" %>
 <%! 
 HelperClass helper = new HelperClass();
 %>
 Helper Stuff: <%= helper.someMethod() %><br/>
 Static Stuff: <%= StaticHelper.someStaticMethod() %>

would this accomplish your goal?

like image 66
Dave Avatar answered Feb 13 '26 17:02

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!