2

is it possible to convert java server pages to servlets ?

user602774
  • 1,093
  • 7
  • 19
  • 31
  • It is possible, however its not gonna be a simple task as you need to take care of rendering all the static html rendering that is in JSP right now along with the inline code... Any reason for trying this? – Chandu Feb 17 '11 at 14:51
  • 1
    Why would you? JSPs **are** Servlets. – JB Nizet Feb 17 '11 at 14:51
  • 4
    Yes, it's possible - the servlet container does this for you at runtime.... – skaffman Feb 17 '11 at 14:51
  • What exactly do you mean? Are you asking how to convert *scriptlets* (the old-fashioned way of writing raw Java code in JSPs) to fullworthy Java classes? If so, then this is related: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files If not, be aware that you should not write HTML/CSS/JS code in a servlet. They should be written in a JSP (which after all get compiled into servlet, but that should not be your concern). – BalusC Feb 17 '11 at 14:56

5 Answers5

3

JSPs are compiled to a Servlet on the server the first time you access them. This is one of the reasons why you need a JDK instead of a JRE when running a Servlet container or Java EE stack... unless the server itself comes with a Java compiler, like Tomcat does.

Depending on the server, there may be methods to precompile JSPs when you initially deploy the application, or if you deploy using Maven, by configuring Maven to use the jspc plugin.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 1
    "This is one of the reasons why you need a JDK instead of a JRE..." - That is not true for all servlet containers. Some contain their own Java compiler; for example, Tomcat contains Eclipse's Java compiler. – Jesper Feb 17 '11 at 15:06
2

jsp files are compiled to servlets automaticly, so you needn't compile by yourself. If you are using Tomcat you can look at this servlets - java classes. It's in:

Tomcat/work/Catalina/localhost/[my-web-app]/org/apache/jsp

Work directory is created when you start your Tomcat server, so before you check, start Tomcat :)

If you are using different container check documentation.

lukastymo
  • 26,145
  • 14
  • 53
  • 66
1

If you want to do the translation on the command line, you could use https://github.com/bnoqlr/jsppreproc which is just a wrapper for jspc. After compiling the wrapper by running mvn package, you can simply run java -jar target/jsppreproc-1.0-SNAPSHOT.jar -d /tmp some.jsp. You will find the servlet (.java file) with inside the /tmp directory.

Julian
  • 1,694
  • 22
  • 29
1

jsp are compiled to servlets. In fact you might have a jsp which does not render anything and which is going to be your servlet. BTW, why do you want to do something like this?

Luixv
  • 8,590
  • 21
  • 84
  • 121
0

Tomcat/work/Catalina/localhost/[my-web-app]/org/apache/jsp

lukastymo he say right but you just rename the class name and file name to JSP convert to Servlets But it is not necessory