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
Related
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.
I am working with a tomcat app with multiple servlets.
I want to be able to initialize these and do dependency injection on server start up.
I understand that I will have to declare a org.springframework.web.servlet.DispatcherServlet .
But I am not sure how should my web.xml look like. Currently it looks like following:
<servlet>
<servlet-name>AddAccount</servlet-name>
<servlet-class>com.addressbook.servlets.AddAccount</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AddAccount</servlet-name>
<url-pattern>/AddAccount</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.addressbook.servlets.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
Currently, any request to add an account is sent directly to /AddAccount and for login the request is sent to /Login.
With DispatcherServler, how should my new web.xml and request structure look like? Do I have to make a new servlet that implements DispatcherServlet and forward each request to this new servlet which then forwards to correct Servlet?
You dont need multiple servlets for several kind of actions in your app. The DispatcherServlet is a front controller that handles all requests and dispatches them to your controllers. As Sotirios suggested, have a look at the Spring MVC manual first. Only the Dispatcher servlet is neccessary.
Currently, we have "root" (/) mapped to a static index.html page, but we want to upgrade to a jsp to have dynamic content. Trying to figure out how to do this. We have content that is mapped to the default content server (e.g. /css), so we don't want to change too much.
We tried:
Changing the .html to .jsp. This resulted in a blank page.
Changing the .html to .jsp and then moving the file into the WEB-INF directory. This resulted in a 404.
Trying to subclass the DefaultServlet class that content servlet is currently mapped to. This through a 500, with a class assertion error (it checked to see if it was the same class).
Adding another servlet to that url, but it overwrote the current one.
I've searched StackOverflow, but still haven't found an answer that works.
Thanks!
If I understand your question correctly, this is trivial using Spring MVC:
<mvc:default-servlet-handler/>
And in web.xml:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/your-applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Is this what you have tried already?
Just set up a controller method mapped to / that returns a view name, which is your jsp file. And make sure your view resolver is set up correctly. Any of the spring mac tutorial hellos world programs out there will show how.
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.
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.