JSP

Java Server Pages

Maven example

   1 cd /tmp
   2 mkdir jspExample
   3 cd jspExample

pom.xml

   1 <project>
   2     <modelVersion>4.0.0</modelVersion>
   3     <groupId>test</groupId>
   4     <artifactId>dummy</artifactId>
   5     <version>1.0</version>
   6     <packaging>war</packaging>
   7     <dependencies>
   8         <dependency> 
   9             <groupId>javax</groupId>
  10             <artifactId>javaee-web-api</artifactId>
  11             <version>6.0</version>
  12             <scope>provided</scope>
  13         </dependency> 
  14     </dependencies>
  15 </project>

   1 mkdir -p src/main/webapp/WEB-INF

nano src/main/webapp/WEB-INF/web.xml

   1 <web></web>

src/main/webapp/index.jsp

   1 <%@page import="org.allowed.bitarus.Handler" %>
   2 <jsp:useBean id="test" class="org.allowed.bitarus.TestBean" />
   3 <% //Handler h = new Handler(request,response,out); %>
   4 <% Handler h = new Handler(pageContext); %>
   5 <html>
   6     <body>        
   7         <p>Hello</p>
   8         <p>World <jsp:getProperty name="test" property="name" /> </p>
   9         <% h.writeHandler(); %>
  10         <h2><% out.print("Your IP address is " + request.getRemoteAddr()); %></h2>
  11         <h3><% h.writeIP(); %></h3>
  12     </body>
  13 </html>

   1  * mkdir -p src/main/java/org/allowed/bitarus

nano src/main/java/org/allowed/bitarus/TestBean.java

   1 /*
   2 mkdir -p src/main/java/org/allowed/bitarus
   3 kate src/main/java/org/allowed/bitarus/TestBean.java
   4 */
   5 package org.allowed.bitarus;
   6 import java.util.logging.Logger;
   7 import java.util.logging.Level;
   8 
   9 public class TestBean{
  10     private String name;
  11     private Logger logger;
  12     
  13     public TestBean(){
  14         this.logger = Logger.getLogger(this.getClass().getName() );
  15         this.logger.log(Level.INFO,"Created test bean");
  16         this.name="Ioooo";
  17     }   
  18     
  19     public String getName(){
  20         this.logger.info("getName called");
  21         return this.name;        
  22     }
  23     
  24     public void setName(String name){
  25         this.name=name;        
  26     }
  27 }

nano src/main/java/org/allowed/bitarus/Handler.java

   1 package org.allowed.bitarus;
   2 
   3 import java.util.logging.Logger;
   4 import java.util.logging.Level;
   5 import javax.servlet.ServletRequest;
   6 import javax.servlet.ServletResponse;
   7 import javax.servlet.jsp.JspWriter;
   8 import javax.servlet.jsp.PageContext;
   9 
  10 /*
  11 mkdir -p src/main/java/org/allowed/bitarus
  12 http://localhost:8081/dummy-1.0/index.jsp?user=TestASD
  13 */
  14 public class Handler{
  15     private ServletRequest request;
  16     private ServletResponse response;
  17     private JspWriter out;
  18     
  19     public Handler(ServletRequest request, ServletResponse response,JspWriter out){
  20         this.request = request;
  21         this.response = response;        
  22         this.out = out;
  23     }
  24 
  25     public Handler(PageContext pageContext){
  26         this.request = pageContext.getRequest();
  27         this.response = pageContext.getResponse();        
  28         this.out = (JspWriter) pageContext.getAttribute(pageContext.OUT) ;
  29     }
  30     
  31     public void writeHandler(){
  32         try{
  33           this.out.println(String.format("<p>Handler up %s</p>" , this.request.getParameter("user") ));
  34         }
  35         catch(Exception ex){            
  36         }          
  37     }
  38     
  39     public void writeIP(){
  40         try{
  41             this.out.print("!!! Your IP address is " + this.request.getRemoteAddr());
  42         }
  43         catch(Exception ex){            
  44         }          
  45     }
  46         
  47 }

Structure:

.
├── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── allowed
        │           └── bitarus
        │               ├── Handler.java
        │               └── TestBean.java
        └── webapp
            ├── index.jsp
            └── WEB-INF
                └── web.xml

8 directories, 5 files

   1 ab -n 100 -c 100 http://localhost:8081/dummy-1.0/index.jsp?user=TestASD

Deploy in

Tomcat Version

JVM Version

JVM Vendor

OS Name

OS Version

OS Architecture

Hostname

IP Address

Apache Tomcat/7.0.53

1.7.0_15-b03

Oracle Corporation

Linux

3.2.45-smp

i386

darkstar

127.0.0.1

   1 git clone https://github.com/vborrego/jsp-example.git

Java/JSP (last edited 2023-05-29 09:06:58 by 127)