app engine simple java webservice - java

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.

Related

Root "/" mades server load index.html before pass through the servlet

I have a Servlet that is mapped to the root directory "/":
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>com.motorola.triage.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In this servet there is a couple of small things that are done there, like authentication and retrieve of Google Plus information. After that, I'm doing a forward to a JSP file called "index.jsp"
req.getRequestDispatcher("index.jsp").forward(req,resp);
When I'm accessing "localhost:8080/" the static file "index.jsp" is loaded without passing through the servlet. For architecture reasons I can not change the name of index.jsp. I would like to ask if is there any way to change this behavior of the server and make it look to the servlet before look the index.jsp file.
This is occurring specifically because you used the name index.jsp.
This has been covered elsewhere, such as here and here and here.

Open a jsp page when path is dynamic

I have a jsp page(with corresponding servlet) that needs to be open whatever the path is specified. For example my jsp is webapps/test/index.jsp
It should be opened as http://localhost/[path] where the path is an arbitrary string.
e.g.localhost/xxxx and localhost/yyyy shall open the same webapps/test/index.jsp.
I am using Tomcat 7.0 and Servlet 3.0.
Can this be implemented by tomcat or servlet configuration? What are the options to do this?
Thanks in advance.
You can map /* to that JSP page by registering a new servlet in your web.xml:
(...)
<servlet>
<servlet-name>index-view</servlet-name>
<jsp-file>/test/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>index-view</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
(...)
More info on Oracle's pages: (LINK)
You can create a servletfilter and in that filter use the requestDispatcher.forward method to forward requests to the target page. You can get the url of the request using
String url = ((HttpServletRequest) request).getRequestURL().toString();
and forward that request using
request.getServletContext().getRequestDispatcher(newUrl)
.forward(request, response);
I think you should check your project's config files,ensure the servlet-mapping is right and unique in the web.xml .

Should url of Ajax's $.post match url in the web.xml

I am trying to send data from js to a servlet.
my js is in webapp\secure folder while the servlet s in java\com\ servlet folder.
I read for sending data using ajax I use
$.post("someservletname", {cityName:"hello"});
In the web.xml
<servlet>
<servlet-name>someservletname</servlet-name>
<servlet-class>java.com.someservletname</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>someservletname</servlet-name>
<url-pattern>/someservletname</url-pattern>
</servlet-mapping>
But when i put a simple sysout.print statement I do not see anything. How do i make sure that the mapping worked properly.Should i give the complete url in the post function call?
Ensure you put in the URL:
context path of your web application
Regards
Philippe

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.

Google App Engine - Blob Store Service with a Dispatcher Servlet

I have a central dispatcher servlet that has a servlet mapping of :
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
When i try to use the blob store service's createUploadUrl("/uploadComplete") it maps to a URL for e.g '/_ah/upload/agp0d2VldG15cGljchsLEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YEgw'.
Before the Blob store service can handle the upload and redirect to /uploadComplete; my dispatcher servlet gets called and i am therefore not being able to upload anything.
Is there a servlet/ filter that i can map to /_ah/upload/* in my web.xml ?
How do i avoid the dispatcher servlet from getting called before the Blob store service can do its thing?
Have you tried mapping your uploadComplete servlet?
I would add a mapping like:
<servlet>
<servlet-name>UploadComplete</servlet-name>
<servlet-class>com.Rahul.test.UploadComplete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadComplete</servlet-name>
<url-pattern>/uploadComplete</url-pattern>
</servlet-mapping>
just before your mapping
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
URLs under /_ah/ are reserved by App Engine, and will be directed to the appropriate subsystem regardless of what your config says. What makes you think requests are being sent to your handler instead of the blobstore one?
On further investigation, i have determined that the handler is getting called, however the request is not getting rewritten to "/uploadComplete" on the DevAppServer.
The blobs get uploaded to the blob store, but the forwarded request from the Blobstore service reads /_ah/upload/...* which is an invalid url as far as my end of the web application is concerned.
Thank you for all your answers thus far.

Categories