Defining a class in a JSP

49,477

Solution 1

I don't see why it wouldn't be possible. A JSP is just another way of writing a Servlet, so you should be able to create classes as static (or for that matter, non-static) inner classes within the Servlet, as you would any other class, using the <%! %> convention.

I was able to do a quick, functional, proof of concept:

<%@page contentType="text/html" pageEncoding="MacRoman"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%!
private static class NdBadIdea {
  private final int foo = 42;

  public int getFoo() {
    return foo;
  }
}
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=MacRoman">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%=new NdBadIdea().getFoo()%>
    </body>
</html>

Solution 2

Just for information: code snippet from the question declares a nested class (i.e. a class, declared inside a method body). It would be legal without public keyword:

<%      
    class Person { 
        ...
    }
%>
Share:
49,477
Vivin Paliath
Author by

Vivin Paliath

Check out regula. It is very neat and will make all your dreams come true. Your monkey poured coffee in my boots! I assume that everyone knows more than I do. map{@n=split//;$j.=$n[0]x$n[1]}split/:/,"01:11:02". ":11:01:11:02:13:01:11:01:11:01:13:02:12:01:13:01". ":11:04:11:06:12:04:11:01:12:01:13:02:12:01:14:01". ":13:01:11:03:12:01:11:04:12:02:11:01:11:01:13:02". ":11:03:11:06:11:01:11:05:12:02:11:01:11:01:13:02". ":11:02:12:01:12:04:11:06:12:01:11:04:12:04:11:01". ":12:03:12:01:12:01:11:01:12:01:12:02:11:01:11:01". ":13:02:11:01:02:11:01:12:02";map{print chr unpack" i",pack"B32",$_}$j=~m/.{8}/g

Updated on February 09, 2020

Comments

  • Vivin Paliath
    Vivin Paliath over 4 years

    Please don't punch me in the face! I know this flies in the face of good design, but I'm simply writing a test page to demonstrate something. Our webapp module (correctly) has no direct access to our domain classes. I don't want to create a whole class outside of the JSP, since the page is just for demonstration purposes, and I don't want to write a lot of extraneous code for the same reason. I was trying to define a class the usual way in the JSP, but that didn't work (threw a lot of compile-time errors). This is a quick-n-dirty, one-time deal (I'll be getting rid of it once I'm done). I'd just like to know if this is possible or not. If not, then I will go the long way.

    <%
    
     public class Person {
        private int id;
        private int age;
        private String name;
    
        /*
          ... ctor and getters and setters
        */
    
     }
    %>
    

    And the errors I got:

    convert-jsp-to-java:
    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD ERROR
    [INFO] ------------------------------------------------------------------------
    
    An error occurred at line: 57 in the generated java file
    Syntax error on token "class", invalid VariableDeclarator
    
    An error occurred at line: 73 in the generated java file
    The return type is incompatible with Object.getClass()
    
    An error occurred at line: 74 in the generated java file
    Syntax error on token "class", Identifier expected
    
    An error occurred at line: 77 in the generated java file
    Syntax error on token "class", invalid VariableDeclaratorId
    
    An error occurred at line: 78 in the generated java file
    Syntax error on token "this", PrimitiveType expected
    
    An error occurred at line: 78 in the generated java file
    Syntax error on token "class", invalid Expression
    
    An error occurred at line: 79 in the generated java file
    Syntax error on token "class", invalid Expression
    
  • Adeel Ansari
    Adeel Ansari over 14 years
    static class is a better idea here, IMO. +1
  • Vivin Paliath
    Vivin Paliath over 14 years
    Ahh, I see what I did wrong - no exclamation mark. Thank you! @Vinegar, yes static does seem like a better option. Thanks!
  • Admin
    Admin almost 10 years
    Damit, I cannot use %><% inside <%! %>. Is this normal?
  • Rohit
    Rohit almost 7 years
    @VivinPaliath With no exclamation mark, your code will go inside a method body, so you can't make it public. You can remove 'public' and the code will work fine with no exclamation as well.