How can I call servlet using window.open()?
My code is:
onclick='window.open("/PDFServlet", "popupWindowName",
"dependent=yes, menubar=no, scrollbars=yes, resizable=1, toolbar=no,width800,height=600")'
This should work, if your application is deployed as the ROOT context. If not, you have to specify /context/PDFServlet/
Related
I have a servlet called Document to which I want to redirect all requests starting with Document something like
http://localhost:8080/CollabEdit/Document/abcdwcklsclds
should be redirected to servlet Document.
So, I used an annotation like this :
#WebServlet("/Document/*")
However, for some unidentified reason, it gives an exception that says:
javax.servlet.ServletException: AS-WEB-CORE-00089
This exception is in Document.java is thrown as soon as I call
request.getRequestDispatched("main.html").forward(request, response) .
otherwise no exception.
however, With same request in other servlets, main.html gets called just fine.
A forward and a redirect are not the same thing. A redirect allows you to go to any URL, eventually on another server or with another protocol, because you ask the client to query that URL.
In a forward you ask the servlet container to pass the control to another servlet of the same application (inside same context). As you use a relative path you are actually requesting what is the servlet for : http://host.do.main/appname/Document/main.html because the relative URL is added at the end of the address of current page (it could even be .../Document/.../main.html) !
And you declared that any page under /Document should be served by the Document servlet ... so the infinite loop ...
You can fix it two ways :
if you really need to use relative paths (it is inherently dangerous but you might have reasons to do it), try ../main.html if called from /Document or ../../main.html if called from /Document/something
use an absolute path :
contextPath = request.getContextPath();
request.getRequestDispatcher(contextPath + "/main.html").forward(request, response)
The Filter looks right.
Are you using Servlets 3.0 and no older version? In older versions you would have to edit the web.xml. And in 3.0 it would have to look like in this Post
Does Document extend HttpServlet?
Could you show us some more of your Code, maybe then it would be easier to see what's going wrong.
Greetings
I need to call a servlet during a page load(abc.jsp). The Servlet would not return anything.It just makes some updates to a database.
If I use href to call the servlet, abc.jsp does not get loaded as the servlet does not return anything.
If I use form submit(empty form with no fields) to call the servlet and call it during onload event of the abc.jsp, that does makes a call to the servlet but abc.jsp will not be loaded.
What is the best way to call the servlet which does not return anything and still load abc.jsp ?
Thanks
Use jQuery Ajax request during page loade time:
$(document).ready(function(){
$.get( "myServlet" );
});
Maybe you should use Filter. As Specification says:
Filters differ from web components in that filters usually do not themselves create a response. Instead, a filter provides functionality that can be “attached” to any kind of web resource. Consequently, a filter should not have any dependencies on a web resource for which it is acting as a filter; this way, it can be composed with more than one type of web resource.
How to call servlet from JSP which is present inside a subpackage?
The servlet is present inside Source Packages" folder. Name of servlet is:
servlets.io.registration.servlet1.java
Now i want to call this servlet from JSP page,
<form name="admin-form" action="/*Path of servlet goes here*/">
But this is not working.
It's simple, for Servlets you can give them an address relative to your context root.
With any J2EE container with the Servlet 3.0 API (for instance Glassfish 4) you can annotate servlets with #WebServlet("path"), setting their path, for instance #WebServlet("/someservlet"). You can even use subpaths, like #WebService("path/sub/someservlet").
So assuming your J2EE application is called "Registration", running on port 8080 and you put this on top of your Servlet class: #WebServlet("/someservlet"), you can set the action to http://yourserver.com:8080/Registration/someservlet
Use WebRequest annotation on your servlet to configure whatever path you like:
#WebServlet(urlPatterns="/myservletpath")
public class MyServlet extends HttpServlet
{
...
http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html
I have this question as i did not find any correct results when searching in Web.
Does Servlet Init Parameters y default accessible in whole container without making any configaration.I mean servlet A has some property x,If another servlet B ,in the same container(may be or may not be in same Application) access it? If so can anyone please show me with an example.
Thanks in Avance.
No, init parameters are servlet-scoped, context-param are application scoped.
There's no other wider scope (e.g. between two applications)
As servlet config, The init parameters of a servlet can only be accessed by that servlet
here is link http://tutorials.jenkov.com/java-servlets/web-xml.html#initParams
I want to write a servlet or filter that automatically maps the url /xxx/yyy/zzz to class XxxYyyZzz.java.
For example the following URLs will map to the following java classes:
/comment/add --> CommentAdd.java
/comment/delete --> CommentDelete.java
/comment/view --> CommentView.java
/search --> Search.java
/viewposts --> Viewposts.java
In addition the servlet or filter must comply with two extra requirements:
The servlet or filter should have a servlet mapping of "/*", I dont want a prefix with several servlets "/comment/*", "/search", etc.
Maybe difficult, but having a servlet mapping of /* should not allow it to override the JSP processing. Meaning, if a class is not found, it should check if a jsp page exists and run it.
I want to know how can this be done using the Servlet API. Please don't refer me to any framework that does the job. Just show me the code.
The classes that are mapped to will follow the command pattern or could be a subclass of the HttpServlet. In both cases, a method should exist like "execute(HttpServletRequest request, and HttpServletResponse response)". This method will be automatically executed once the URL is accessed and the java class is figured out possibly using a single servlet or filter.
I'm not sure, if I got what you mean. In case I did:
You need nothing special, write a single Servlet mapped to "/", so it gets everything. Parse the PATH_INFO (don't know now how it gets called in Java), use Class.forName (or use a pre-filled Map), and call its method execute.
Here is a http://www.tuckey.org/urlrewrite/ filter implementation that might help you. Check it out. I have not used it myself though.
You can use Stripes framework with its default NameBasedActionResolver config.