Jersey servlet mapping confusion - java

Can somebody please explain in plain English how this works (or at least why my structure doesn't)? I want a function called ExportLicenseInfo in my Jersey servlet resource, which I have called ExportResource, to map to http://example.com/myApp/export/software_licenses.{year}-{month}.{format}, for example: http://example.com/myApp/export/software_licenses_2013-10.csv
Servlet config in web.xml:
<servlet>
<servlet-name>ExportServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mycompany.app.ExportApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
My servlet mapping in web.xml:
<servlet-mapping>
<servlet-name>ExportServlet</servlet-name>
<url-pattern>/export</url-pattern>
</servlet-mapping>
My code with #Path annotations:
#Path("/export")
public class ExportResource {
...
#GET
#Produces({"text/csv", "application/json"})
#Path("/software_licenses_{year: [0-9][0-9][0-9][0-9]}-{month: [0-1][0-9]}.{format}")
public String ExportLicenseInfo( ... ) {
...
}
When I try to access the resource, I get a 404. Jersey trace log:
Feb 21, 2014 2:41:46 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 6 * LoggingFilter - Request received on thread http-bio-8080-exec-67
6 > GET http://localhost:8080/app/export/software_licenses_2013-10.csv
6 > host: localhost:8080
6 > connection: keep-alive
6 > cache-control: max-age=0
6 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
6 > user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
6 > accept-encoding: gzip,deflate,sdch
6 > accept-language: en
6 > cookie: JSESSIONID=DB64E0B066BDEE8CABFC94686AD6ACDC.test; JSESSIONIDSSO=61B51CEED61F8F6CE8A8DB46B38BAC9F; i18next=en
Feb 21, 2014 2:41:46 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 6 * LoggingFilter - Response received on thread http-bio-8080-exec-67
6 < 404

Try mapping the jersey servlet like this:
<servlet-mapping>
<servlet-name>ExportServlet</servlet-name>
<url-pattern>/export/*</url-pattern>
</servlet-mapping>

Related

Missing set-cookie header on POST method

When i perform a request using HTTP POST method from Angular, response comes without set-cookie header, contrary when I perform a request using HTTP GET method response comes with set-cookie Header.
I know that when POST method is used a preflight OPTIONS request is automatically issued by a browser. I am looking for advice what can i make so after POST method request client get set-cookie header as it already works with GET method?
My Angular request:
this.http.post<UserDto>("//localhost:8080/users", this.user, {withCredentials: true}).subscribe( user => {
alert("User created successfully.");
});;
Those are response headers i get:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: POST,PUT,DELETE,GET,OPTIONS
Access-Control-Allow-Origin: http://localhost:4200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 0
Date: Sun, 05 May 2019 09:22:55 GMT
Expires: 0
Pragma: no-cache
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
And my request headers:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: localhost:8080
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
My Spring Security configuration:
#Configuration
#EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.
cors().and().
csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and().addFilterBefore(
new StatelessCSRFFilter(), CsrfFilter.class);
}
#Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(ImmutableList.of("http://localhost:4200"));
configuration.setAllowedMethods(Arrays.asList("POST","PUT","DELETE","GET","OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
I have found such an advice:
" I got it to work by adding a response filter for the pre-flight OPTIONS request that works without credentials.
<security-constraint>
<display-name>No check for options</display-name>
<web-resource-collection>
<web-resource-name>All Access</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
I am not sure how to implement that solution is spring.
The CORS configuration has been set up correctly on the SpringFramework backend, as the methods succeed as expected, and Angular does set the cookies for GET requests. It is only POST operations that do not succeed if not preceded by HTTP.GET request.

Error 503 in GWT with the servlet and paths (Java)

I work with IntelliJ and when I run it in a web browser, it returns 503 error :
Problem accessing / MySampleApplication.html. Reason:
Service Unavailable"*
The bug should be somewhere in the XML file and it seems to be something wrong with the servlet path.
XML:
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.8.0//EN"
"http://gwtproject.org/doctype/2.8.0/gwt-module.dtd">
<module rename-to="MySampleApplication">
<inherits name='com.google.gwt.user.User'/>
<entry-point class='com.mySampleApplication.client.MySampleApplication'/>
<servlet path='/MySampleApplicationService' class='com.mySampleApplication.server.ServiceImpl'/>>
/>
</module>
Console:
Code server started in 12.55 s ms
2019-03-06 13:09:20.410:INFO:oejs.Server:main: Started #13836ms
Loading modules
com.mySampleApplication.MySampleApplication
Validating <servlet> tags for module 'MySampleApplication'
For additional info see: file:/Users/simonemettesorensen/Downloads/gwt-2.8.2/doc/helpInfo/servletMappings.html
[WARN] Module declares a servlet class 'com.mySampleApplication.server.ServiceImpl' with a mapping to '/MySampleApplication/MySampleApplicationService', but the web.xml has no corresponding mapping; please add the following lines to your web.xml:
<servlet-mapping>
<servlet-name>com.mySampleApplication.MySampleApplication Service</servlet-name>
<url-pattern>/MySampleApplication/MySampleApplicationService</url-pattern>
</servlet-mapping>
2019-03-06 13:09:20.549:INFO:oejs.Server:main: jetty-9.2.z-SNAPSHOT
Starting Jetty on port 8888
[WARN] Failed startup of context c.g.g.d.s.j.WebAppContextWithReload#41aa7994{/,file:/Users/simonemettesorensen/Library/Caches/IntelliJIdea2018.2/gwt/%C3%B8velsemedrpc.9e5e27b2/oevelseMedRPC.6c3e6f2f/run/www/,STARTING}{/Users/simonemettesorensen/Library/Caches/IntelliJIdea2018.2/gwt/øvelsemedrpc.9e5e27b2/oevelseMedRPC.6c3e6f2f/run/www}
java.lang.NullPointerException
at java.lang.String.compareTo(String.java:1155)
at org.eclipse.jetty.servlet.ServletHolder.compareTo(ServletHolder.java:209)
at org.eclipse.jetty.servlet.ServletHolder.compareTo(ServletHolder.java:70)
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:321)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.util.Arrays.sort(Arrays.java:1246)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:865)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:298)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1349)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:741)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:505)
at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:550)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.handler.RequestLogHandler.doStart(RequestLogHandler.java:140)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.server.Server.start(Server.java:387)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.Server.doStart(Server.java:354)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:760)
at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:636)
at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:923)
at com.google.gwt.dev.DevModeBase.run(DevModeBase.java:706)
at com.google.gwt.dev.DevMode.main(DevMode.java:432)
2019-03-06 13:09:22.470:INFO:oejs.ServerConnector:main: Started ServerConnector#1006dd06{HTTP/1.1}{127.0.0.1:8888}
2019-03-06 13:09:22.470:INFO:oejs.Server:main: Started #15896ms
Dev Mode initialized. Startup URL:
http://127.0.0.1:8888/MySampleApplication.html
[ERROR] 503 - GET /MySampleApplication.html (127.0.0.1) 312 bytes
Request Headers
Host: 127.0.0.1:8888
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: da-DK,da;q=0.9,en-US;q=0.8,en;q=0.7
Response headers
Date: Wed, 06 Mar 2019 12:09:22 GMT
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html; charset=ISO-8859-1
Content-Length: 312
And if I add this, it won't run because of an unexpected error: "servlet mapping :
<servlet-mapping>
<servlet-name>com.mySampleApplication.MySampleApplication Service</servlet-name>
<url-pattern>/MySampleApplication/MySampleApplicationService</url-pattern>
</servlet-mapping>
I would really appreciate any help.
Simone, welcome to the GWT world!
If that servlet uses GWT-RPC I would strongly suggest you to look into creating a JSON endpoint. Moving forward the RPC part will be dropped, JSON is standard and future-proof.
It's also waaaaaaay easier to debug and mock up, makes working with GWT significantly easier.
You need to replace the <servlet> from your *.gwt.xml file with a <servlet-mapping> (and <servlet>) in your WEB-INF/web.xml (a standard servlet mapping, not GWT-specific, like in any other, non-GWT, servlet development)

Jersey Servlet not found after switching to Spring

I have a simple RESTful Java web service...
#Service
#Path("/getAccountBalance")
public class GetAccountBalanceService {
#Autowired
private ILicenseService licenseService;
#GET
#Path("/{param}")
public Response provideService(#PathParam("param") String licenseUUID) {
License license = this.licenseService.getByUUID(licenseUUID);
String output = "Balance on the account : " + license.getBalanceValue();
return Response.status(200).entity(output).build();
}
At first I was getting an error where the bean licenseService was null even though I am autowiring it. So based on advice from another post I swtiched to use the Spring servlet for jersey, thus in my web-xml I changed from com.sun.jersey.spi.container.servlet.ServletContainer to...
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.freedomoss.crowdcontrol.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
But now when I start Tomcat I am getting the error..
Dec 19, 2014 12:13:13 AM org.apache.catalina.startup.ContextConfig applicationWebConfig
SEVERE: Parse error in application web.xml file at jndi:/localhost/mturk-web/WEB-INF/web.xml
java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name jersey-serlvet
Why is this happening? As you can see there is a servlet defined with that name in web-xml. If there was something wrong with loading that servlet why don't the logs tell that story?
Thanks.

CGI script error java RMI

Im trying to set up a cgi script for my RMI webserver, but it is giving me an HTTP 500 all the time.
My CGI script look like this.
# This class will support a QUERY_STRING of the form "forward=<port>"
# with a REQUEST_METHOD "POST". The body of the request will be
# forwarded (as another POST request) to the server listening on the
# specified port (must be >= 1024). The response from this forwarded
# request will be the response to the original request.
#
# CONFIGURATION:
#
# Fill in correct absolute path to Java interpreter below. For example,
# the "PATH=" line might be changed to the follow if the JDK is installed
# at the path "/home/peter/java":
#
# PATH=/home/peter/java/bin:$PATH
#
PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:$PATH
exec java \
-DAUTH_TYPE="$AUTH_TYPE" \
-DCONTENT_LENGTH="$CONTENT_LENGTH" \
-DCONTENT_TYPE="$CONTENT_TYPE" \
-DGATEWAY_INTERFACE="$GATEWAY_INTERFACE" \
-DHTTP_ACCEPT="$HTTP_ACCEPT" \
-DPATH_INFO="$PATH_INFO" \
-DPATH_TRANSLATED="$PATH_TRANSLATED" \
-DQUERY_STRING="$QUERY_STRING" \
-DREMOTE_ADDR="$REMOTE_ADDR" \
-DREMOTE_HOST="$REMOTE_HOST" \
-DREMOTE_IDENT="$REMOTE_IDENT" \
-DREMOTE_USER="$REMOTE_USER" \
-DREQUEST_METHOD="$REQUEST_METHOD" \
-DSCRIPT_NAME="$SCRIPT_NAME" \
-DSERVER_NAME="$SERVER_NAME" \
-DSERVER_PORT="$SERVER_PORT" \
-DSERVER_PROTOCOL="$SERVER_PROTOCOL" \
-DSERVER_SOFTWARE="$SERVER_SOFTWARE" \
sun.rmi.transport.proxy.CGIHandler
In my access.log it says:
"POST /cgi-bin/java-rmi.cgi?forward=1099 HTTP/1.1" 500 415 "-" "Java/1.7.0"
In my error.log it says:
(104)Connection reset by peer: ap_content_length_filter: apr_bucket_read() failed
I am using RMISocketFactory.setSocketFactory(new sun.rmi.transport.proxy.RMIHttpToCGISocketFactory()); to force Http-to-cgi.
I can't figure out what i'm doing wrong..
I have put my java-rmi.cgi file in /usr/lib/cgi-bin/
Am i suppose to add something else to the script?
The java error i get is:
java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.IOException: HTTP request failed
EDIT: Visiting my cgi script as a website it give me(maybe you can't do that):
Java RMI Client Error
invalid command.
I traced this into the CGICommandHandler.java and it returns invalid command when the handler is null.
This is the debug output when i run bash -x:
+ PATH=/usr/lib/jvm/java-7-openjdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
+ exec java -DAUTH_TYPE= -DCONTENT_LENGTH= -DCONTENT_TYPE= -DGATEWAY_INTERFACE= -DHTTP_ACCEPT= -DPATH_INFO= -DPATH_TRANSLATED= -DQUERY_STRING= -DREM OTE_ADDR= -DREMOTE_HOST= -DREMOTE_IDENT= -DREMOTE_USER= -DREQUEST_METHOD= -DSCRIPT_NAME= -DSERVER_NAME= -DSERVER_PORT= -DSERVER_PROTOCOL= -DSERVER_S OFTWARE= sun.rmi.transport.proxy.CGIHandler
Status: 400 Bad Request: invalid command.
Content-type: text/html
<HTML><HEAD><TITLE>Java RMI Client Error</TITLE></HEAD><BODY>
<H1>Java RMI Client Error</H1>
invalid command.
</BODY></HTML>
And if i echo out my cgi i file i get this:
AUTH_TYPE=
CONTENT_LENGTH=
CONTENT_TYPE=
GATEWAY_INTERFACE=CGI/1.1 HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 PATH_INFO=
PATH_TRANSLATED=
QUERY_STRING=
REMOTE_ADDR=xx.xxx.x.xx
REMOTE_HOST=
REMOTE_IDENT=
REMOTE_IDENT REMOTE_USER=
REQUEST_METHOD=GET SCRIPT_NAME=/cgi-bin/java-rmi.cgi.sh
SERVER_NAME=xx.xxx.xxx.xxx
SERVER_PORT=80 SERVER_PROTOCOL=HTTP/1.1
SERVER_SOFTWARE=Apache/2.2.22 (Ubuntu) Status: 400 Bad Request: invalid command. Content-type: text/html
As you can see my many strings are empty...
EDIT 2: Now my acess log brings me this:
"POST http://xx.xxx.xxx.xxx/cgi-bin/java-rmi.cgi?forward=1099 HTTP/1.1" 500 415 "-" "Java/1.7.0"
Still HTTP 500, my error log gives my the same as before..
EDIT 3:
I now tried to implement the servlet of java-rmi.cgi and it's giving me errors.
My http.conf lookes like this:
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_ajp_module /usr/lib/apache2/modules/mod_proxy_ajp.so
<Location /cgi-bin/java-rmi.cgi>
ProxyPass ajp://localhost:8009/rmi/ServletHandler
</Location>
What should be the port and adress? In the guide it says localhost:8009(https://forums.oracle.com/message/4804030).
This is where i've placed ServletHandler.class:
/var/lib/tomcat7/webapps/rmi/WEB_INF/classes
My web.xml(inside WEB_INF) looks like this:
<servlet>
<servlet-name>ServletHandler</servlet-name>
<servlet-class>ServletHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletHandler</servlet-name>
<url-pattern>/ServletHandler</url-pattern>
</servlet-mapping>
I've installed tomcat 7 properly.
My access log gives me the same but my error log now gives me(with localhost:8009 set in httpd.conf):
[Fri Nov 15 08:16:26 2013] [error] [client 127.0.0.1] Invalid method in request \x124\x01\xb2\x02\x02
[Fri Nov 15 08:16:26 2013] [error] ajp_check_msg_header() got bad signature 3c21
[Fri Nov 15 08:16:26 2013] [error] ajp_ilink_receive() received bad header
[Fri Nov 15 08:16:26 2013] [error] ajp_read_header: ajp_ilink_receive failed
[Fri Nov 15 08:16:26 2013] [error] (120007)APR does not understand this error code: proxy: read response failed from (null) (localhost)
Error log when setting my global ip and port 80 in http.conf:
[Fri Nov 15 08:13:26 2013] [error] [client xx.xxx.xxx.xxx] Invalid method in request \x124\x01\xb2\x02\x02
[Fri Nov 15 08:13:26 2013] [error] ajp_check_msg_header() got bad signature 3c21
[Fri Nov 15 08:13:26 2013] [error] ajp_ilink_receive() received bad header
[Fri Nov 15 08:13:26 2013] [error] ajp_read_header: ajp_ilink_receive failed
[Fri Nov 15 08:13:26 2013] [error] (120007)APR does not understand this error code: proxy: read response failed from (null) (xx.xxx.xxx.xxx)
EDIT 4:
Now it lookes like this:
web.xml:
<servlet>
<servlet-name>ServletHandler</servlet-name>
<servlet-class>rmiservlethandler.ServletHandler</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletHandler</servlet-name>
<url-pattern>/ServletHandler</url-pattern>
</servlet-mapping>
httpd.conf:
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_ajp_module /usr/lib/apache2/modules/mod_proxy_ajp.so
<Location /cgi-bin/java-rmi.cgi>
ProxyPass ajp://xx.xxx.xxx.xxx:8009/rmi/ServletHandler
</Location>
I tried localhost:8009 but that gives me error, entering my servers global adress makes my webserver to halt, HTTP 503. Stop responding.
My error log for localhost:
[Fri Nov 15 12:20:53 2013] [error] (111)Connection refused: proxy: AJP: attempt to connect to 127.0.0.1:8009 (localhost) failed
[Fri Nov 15 12:20:53 2013] [error] ap_proxy_connect_backend disabling worker for (localhost)
[Fri Nov 15 12:20:53 2013] [error] proxy: AJP: failed to make connection to backend: localhost
error log for global ip:
[Fri Nov 15 12:37:23 2013] [error] (110)Connection timed out: proxy: AJP: attempt to connect to xx.xxx.xxx.xxx:8009 (xx.xxx.xxx.xxx) failed
[Fri Nov 15 12:37:23 2013] [error] ap_proxy_connect_backend disabling worker for (xx.xxx.xxx.xxx)
[Fri Nov 15 12:37:23 2013] [error] proxy: AJP: failed to make connection to backend: xx.xxx.xxx.xxx
Tomcatlog gives me this:
My tomcatlog gives me this:
xx.xxx.x.xx - - [15/Nov/2013:13:32:14 +0000] "POST /rmi/ServletHandler?forward=1099 HTTP/1.1" 404 987
EDIT 5.
Didn't work, i get 404 for /rmi/ServletHandler.
Here is my httpd.conf(taking away .cgi):
<Location /cgi-bin/java-rmi> ProxyPass ajp://localhost:8009/rmi/ServletHandler </Location>
This is web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet>
<servlet-name>ServletHandler</servlet-name>
<servlet-class>rmiservlethandler.ServletHandler</servlet-class>
<load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>ServletHandler</servlet-name>
<url-pattern>/ServletHandler</url-pattern>
</servlet-mapping>
</web-app>
The folder setup is now:
/var/lib/tomcat7/webapps/rmi/WEB_INF.
WEB_INF contains web.xml and classes/rmiservlethandler/ where rmiservlethandler is where all my class files are(package structure).
Why isn't this working?
My /usr/lib/cgi-bin/ is empty that is should be right?
My servlet dosen't run...
Thanks!
Solved it!
My folder setup were correct(except WEB-INF instead of WEB_INF), the problem was that my localhost adress in ServletHandler wasn't resolved properly(my localhost name wasn't localhost..).
Other things like WEB_INF should be WEB-INF...
Following this gave my exceptions in tomcat catalina.out and local host log files.
So if you think your servlet is running properly, look there for exceptions. I modified my ServletHandler with logs so i could se everything that was going on.
This is what my POST from the client looks now inside tomcat logs:
xx.xxx.x.xx - - [18/Nov/2013:13:17:23 +0000] "POST /rmi/ServletHandler?forward=1099 HTTP/1.1" 200 1
My http.conf:
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_ajp_module /usr/lib/apache2/modules/mod_proxy_ajp.so
<Location /cgi-bin/java-rmi.cgi>
ProxyPass ajp://localhost:8009/rmi/ServletHandler
</Location>
My web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ServletHandler</servlet-name>
<servlet-class>com.foo.bl.server.ServletHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletHandler</servlet-name>
<url-pattern>/ServletHandler</url-pattern>
</servlet-mapping>
</web-app>
Folder structure:
/var/lib/tomcat7/webapps/rmi/WEB-INF/classes/
Inside classes you put the same folder structure you have in your java IDE.
Web.xml should be placed inside WEB-INF.
Any lib files needed should be placed in WEB-INF/lib.
Hope it helps someone out there!
Regards Gustav

tomcat not sending the requested http-header

I have got a servlet deployed under tomcat/webapps directory and I try to send a curl request to it:
$~ curl -v http://localhost:8080/
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* Adding handle: conn: 0x33e0ee0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x33e0ee0) send_pipe: 1, recv_pipe: 0
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 302 Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=<ID>; Path=/; HttpOnly
< Location: http://localhost:8080/
< Content-Type: text/html;charset=EUC-KR
< Content-Length: 0
< Date: Mon, 02 Sep 2013 14:24:53 GMT
<
* Connection #0 to host localhost left intact
This is the web.xml file:
...
<servlet>
<description></description>
<display-name>vController</display-name>
<servlet-name>vController</servlet-name>
<servlet-class>com.vc.cac.controller.vlController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vController</servlet-name>
<url-pattern>/vController</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.vc.cac.controller.vlController</listener-class>
</listener>
...
in the servlet I tried to print the Host element from header:
String host = request.getHeader("Host");
System.out.println("Host: "+host);
but I get null, so If I try to print any header element I get null as a result. any help?
On Tomcat 7.0.22, the following servlet
#WebServlet(urlPatterns = "/vController")
public class vlController extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
String host = request.getHeader("Host");
System.out.println("Host: "+host);
resp.getWriter().write("host printed");
}
}
with an index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/vController" />
Heello wRoldasdas
</body>
</html>
The cURL command prints
$ clear; curl -v http://localhost:8080/
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* 0x60002d0f0 is at send pipe head!
* STATE: CONNECT => WAITCONNECT handle 0x60006cac0; line 1032 (connection #0)
* Connected to localhost (127.0.0.1) port 8080 (#0)
* STATE: WAITCONNECT => DO handle 0x60006cac0; line 1151 (connection #0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x60006cac0; line 1236 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x60006cac0; line 1352 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x60006cac0; line 1363 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=7694BACEC0C311357E72DCEA7574540A; Path=/; HttpOnly
Set-Cookie: JSESSIONID=7694BACEC0C311357E72DCEA7574540A; Path=/; HttpOnly
< Content-Type: text/html;charset=ISO-8859-1
Content-Type: text/html;charset=ISO-8859-1
< Content-Length: 12
Content-Length: 12
< Date: Mon, 02 Sep 2013 16:40:11 GMT
Date: Mon, 02 Sep 2013 16:40:11 GMT
<
* STATE: PERFORM => DONE handle 0x60006cac0; line 1533 (connection #0)
* Connection #0 to host localhost left intact
host printed
And the server clearly prints
Host: localhost:8080
In your case, the server responds with 302, so your servlet is never hit. I don't know where your null header is coming from.
Instead of with cURL, try with a Java HTTP client, ex. Apache's HttpClient.

Categories