making the welcome page of a website to be a servlet - java

Can I make the welcome-file of the website to be a servlet ? If yes , how ? I was trying something like :
<welcome-file-list>
<welcome-file>FilterForwarded</welcome-file>
</welcome-file-list>
<!-- FilterForwarded is a servlet -->
While deploying I do not see any error but when I try to open the website abc.com I get a message from the browser that it is unable to connect to this website.Why is it so ?
I want when anyone visits the website,I should be able to store the client's public IP. To do this I wrote a Filter which after taking the IP , passed it to the servlet (from there I could update the logs). After storing the IP , client be automatically redirected to index.jsp. Is there any way to achieve this ?
EDIT :
<servlet-mapping>
<servlet-name>FilterForwarded</servlet-name>
<url-pattern>/FilterForwarded</url-pattern>
</servlet-mapping>
This is the mapping defined in web.xml . When I use /FilterForwarded in welcome-file I get this message when I try to deploy : Bad configuration: Welcome files must be relative paths: /FilterForwarded
From the logs :
com.google.apphosting.utils.config.AppEngineConfigException: Welcome files must be relative paths: /FilterForwarded
at com.google.apphosting.utils.config.WebXml.validate(WebXml.java:125)
at com.google.appengine.tools.admin.Application.<init>(Application.java:150)
at com.google.appengine.tools.admin.Application.readApplication(Application.java:225)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:145)
at com.google.appengine.tools.admin.AppCfg.<init>(AppCfg.java:69)
at com.google.appengine.tools.admin.AppCfg.main(AppCfg.java:65)

If you map the filter to /* you should be able to intercept all requests and then log the IP from there.
Or is your requirement to only log Client IP for the landing page?
If so, you could change the default servlet for the Servlet container, but bear in mind this will change the default servlet for all requests that do not match mappings in your web.xml.
<servlet-mapping>
<servlet-name>FilterForwarded</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
A more complex, but potentially better solution, is to front your Java web container with a web server and use rewrite rules to proxy to your backend Servlets. This way will mean that you can control the Servlet that is accessed for your landing page without overriding the default servlet for all non-matching requests. This might be overkill for your problem though.

Related

Why I am getting 404 error while hiding .jsp extension in web.xml file?

I configured all my JSP page in web.xml file to hide .jsp extension and I created custom URL in web.xml file corresponding to each JSP page due to SEO point of view but when I deployed this application at server tomcat which is running under Apache web server so my server team replies me like this but I am not getting what does they mean.
Please note that you are running in a server where apache is the web
server and Tomcat is a servlet container. This way all static requests
such as images, CSS, js, HTML are handled by apache and jsp, servlets
are handled by Tomcat. This means Apache will forward any request that
you send with the following extension to tomcat
.jsp
/servlet
.do
This means in order for Tomcat to execute your code you need to send a
request to apache as .jsp, /servlet and .do. Once you send this way,
it will automatically send to tomcat to running there. In your case
/hosting will be executed by apache only and that's why you get this
error of 404.
web.xml
<servlet>
<servlet-name>domain</servlet-name>
<jsp-file>/domain-registration.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>domain</servlet-name>
<url-pattern>/domain-registration</url-pattern>
</servlet-mapping>
Do give the proper error, on which url you are getting 404?
Try any other url-pattern you have already specified in the web.xml, check if it works properly or not. I think there is some syntax mistake in your web.xml file other wise it should work.

app engine simple java webservice

I like to expose one JAVA method as a Web service that will accept POST ,strip the parameters out of it and reply accordingly. I read I have to use doPost(req,resp) , but How can I get to the servlet code? what should be in web.xml? there will not be a welcome-file ? After mapping the servlet, can I read it without the need for a index.html as start point?
create the doPost(req,resp) method in your servlet and map it to a url in web.xml
<servlet>
<servlet-name>HelloPost</servlet-name>
<servlet-class>packageName.HelloPost</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloPost</servlet-name>
<url-pattern>/post-url</url-pattern>
</servlet-mapping>
then you can post your request to /post-url .You don't need to use index.html.Any url can be put in welcome file to load for the url /
To set /post-url as landing page , use
<welcome-file-list>
<welcome-file>/post-url</welcome-file>
</welcome-file-list>
you can get started here https://developers.google.com/appengine/docs/java/gettingstarted/creating
If you want to know how to set the web.xml to start the servlet then may be this will help you.

redirecting requests based on the body tomcat

I have a tomcat 7 application which I can get requests from external sources.
Most of them call my request like this:
http://localhost:8080/MyWeb/exRequest
and I build servlet with URL pattern inside MyWeb app.
However, one external source must send the request like this:
http://localhost:8080/
and in the body:
<xml name="test" />
Since I don't want to declare a general servlel (like tomcat default) since it means that any request will need to go through my servlet, I thought to change index.jsp of ROOT to redirect to my servlet.
Is it the best option?
Is there an option to create a default servlet that will be invoked only if there is a special parameter in the body?
EDITED
Please note that I get the requests to localhost:8080 and not localhost:8080/MyWeb - it's general to tomcat and not to a specific web app
You can't choose a servlet to invoke based on the request body, but you can set a servlet as the "welcome-file" in your web.xml.
<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.y.MyWelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
If you want to preserve the "welcome" function of some existing index.jsp, your servlet could forward requests without the correct XML in the body to an index.jsp file located under the WEB-INF directory.
No, but you can create a Filter and forward/redirect to a specific servlet whenever the request meets certain conditions.
If using servlet 3.0 map it with #WebFilter, otherwise use web.xml and <filter> + <filter-mapping>. You should map it be executed before the default servlet.

Remove Foobar.xhtml from URL with JSF

I have written a facelets web application using tomcat as a application server. My program has a foobar.xhtml and the URL to it is:
http://localhost:8080/Myapplication/foobar.faces
Can I change something in my application so that a link to:
http://localhost:8080/Myapplication/
..will actually render my application on http://localhost:8080/Myapplication/foobar.faces ?
Alternatively, could the http://localhost:8080/Myapplication/ be redirected to http://localhost:8080/Myapplication/foobar.faces ?
You would normally use the <welcome-file> entry in the web.xml for this. But unfortunately this doesn't work as expected on at least Tomcat when using fictive URL's which are to be passed through a servlet like a FacesServlet. Tomcat will scan for the physical file on the disk matching the exact name before forwarding. If it isn't present, then you will just face a default 404 error page.
Using /foobar.xhtml as <welcome-file> is also not going to work since that page requires to be parsed by the FacesServlet to get all the JSF stuff to work.
One of the ways to fix this is to place another real /foobar.faces file there next to the real /foobar.xhtml file. It doesn't need to be filled with code, it can be left empty. Just the presence of the physical file is enough for Tomcat to open the desired page as welcome page.
web.xml has a
<welcome-file-list>
<welcome-file>foobar.faces</welcome-file>
</welcome-file-list>
element where you can define the page to be opened.

Disable JSP extension processing

I have a JavaEE 1.4 web application running on WebSphere Application Server 6.0. In web.xml, there is a servlet configured to intercept all server requests:
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This works fine until I try to request something ending with *.jsp. In this case, server tries to find JSP with this name and fails with the error:
java.io.FileNotFoundException: JSPG0036E: Failed to find resource /cfvct/search_criteria.jsp
at com.ibm.ws.jsp.webcontainerext.JSPExtensionProcessor.findWrapper(JSPExtensionProcessor.java:279)
at com.ibm.ws.jsp.webcontainerext.JSPExtensionProcessor.handleRequest(JSPExtensionProcessor.java:261)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3226)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:253)
at com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:229)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1970)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:120)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:434)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:373)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:253)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminaters(NewConnectionInitialReadCallback.java:207)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:109)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:566)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:619)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:952)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1039)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1475)
I need to have this request processed by the servlet, but seems server uses some JSPExtensionProcessor to process all paths ending with .jsp. Is there any way to change this behaviour?
Yes, you'll need to map your servlet to *.jsp in order to get *.jsp support redirected to your servlet.
<servlet-mapping>
<servlet-name>name</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
It is normally a bad idea to have jsps accessible directly, however. Placing them in WEB-INF in some directory, then mapping an appropriate url (.do, .action, etc) to a servlet that then redirects internally to that JSP is the better practice.
So instead of typing thisUrl.jsp, the user would type thisUrl.do or thisUrl.action, and it would then get hit by the servlet to redirect to thisUrl.jsp.

Categories