Redirect to another servlet in java - java

I am having troubles of directing to another servlet in a servlet file. I have a servlet file called NewDreamServlet.java and I want to redirect it to MyDreamsServlet.java.
This is what I currently have in the NewDreamServlet.java for redirecting.
request.getRequestDispatcher("/MyDreamsServlet").forward(request, response);
When I call this it ends up going to a blank page,
http://localhost:8080/ps10-austint/NewDreamServlet
How exactly would I accomplish this? Please let me know if there is any misunderstanding.

Did you try: response.sendRedirect("/YourApp/MyDreamsServlet")

Please try response.sendRedirect("/MyDreamsServlet"). Also, please note that you might have to add an return statement. The following post discusses this in more details java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

All these answers to your question are wrong.
1. if you like to use RD().forward, which is more used for with in application calls, all you need to do is go to your web.xml file and for the url part of your 2nd servlet give it any name you would like eg. /fireServletTwo....
Now come back to your 1st servlet and in the getRqstDispatcher braces, write("/fireServletTwo"); this will tell the xml file to look for a servlet mapping with that name and run that servlet.
2. if you would like to use send.Redirect(); which takes a URL and is used to mostly pass controls outside of the application to another domain, its simple.. DO NOT USE A SLASH /.... just write the name of your servlet2 inside "";
Hope that helps

This one works for me but it usually better to have the context path:
response.sendRedirect(request.getContextPath() + "/home.jsp");

Related

JAX-RS #Path with allowed slashes collide with other resources

I am trying to allow slashes in my Path:
#Path("/user/{login}/points")
by using this solution so it becomes:
#Path("/user/{login : .+}/points")
but it will not work anytime a user names their account e.g
test/points
because only "test" will be passed. It works fine with test/test or any other login not ending with /points.
I have no idea how should I solve this. I need it as String param and it's set by user.
Thanks for any help!
Looks like I found the solution myself with a little help of #YCF_L (sadly he deleted his comment).
Solution:
#Path("/user/{login : .+(?=\\/points)}")

How to hit the rest endpoint that has `:.*` as a part of the path param field

I am using the Stash's REST API in my project. My task is to get the tag details for a specific tag. After checking the Stash's REST API documentation, I found the correct endpoint that I should be using. It is
/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags/{name:.*}
Please see this link for the Stash's REST API documentation.
There is one more endpoint /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags
With this endpoint I am able to retrieve all the tags. The StashTag object looks something like this.
{
"id": "refs/tags/v4.0.0",
"displayId": "v4.0.0",
"latestChangeset": "234dadf41742cfc2a10cadc7c2364438bd8891c5",
"latestCommit": "234dadf41742cfc2a10cadc7c2278658bd8891c5"
"hash" : "null"
}
My first problem is, I don't know which field to use as the parameter for {name:.*}. Should it be the displayId or Id or anything else.
The second problem is, I don't understand what it means to have : (colon) followed by a . (dot) in the endpoint /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags/{name:.*}.
Can someone explain me what is the purpose of :. in the path param and how to hit this kind of an endpoint. Also an example of the complete endpoint would be nice.
So far I have tried hitting
https://stashtest.abc.com/rest/api/1.0/projects/KARTIK/repos/kartiks-test-repository/tags/v4.0.0
https://stashtest.abc.com/rest/api/1.0/projects/KARTIK/repos/kartiks-test-repository/tags/refs/tags/v4.0.0
None of these endpoints work.
Any help is appreciated.
The {name:.*} is really just saying that the field name can be anything. Chalk this one up to poor documentation on their part. Think of it like Regex field, because that's exactly what it is. I'm sure at one point they had something like ^[0-9] then went back and changed it when they realized using only tag numbers would omit anyone using their lightweight tag features.
Remove the v from your tag version and see if that helps. If it does not, I would also recommend creating a lightweight tag (something like mytag) and seeing if you can hit it that way (i.e., /kartiks-test-repository/tags/mytag).
But looking at that documentation is telling me that your v in your tag name is throwing off the REST call.

Not getting request parameter value thru chrome/firefox. Related to double escaping?

Here is my link
"customer.action?custId=211&custAddressId=2341";
This url string is the output of c:out tag in jsp.
In Firefox/Chrome I am getting the value of custAddressId as null. Reason I think(almost sure) is happening because Chrome/Firefox
are escaping the already ecsaped value(thats why I am getting the second paramter name as amp;custAddressId instead of custAddressId)
but IE does not do that.
Is there a way i can configure my tomcat 6 to get the right paramters(even if it is double escaped).
If not is there a way i can handle it in c:out or browser level itself where i do not need to browser specific code ?
No. There is no way you can configure Tomcat to do double unescaping. You need to make sure your client is sending the correct data.

Why getRequestDispatcher("/index.jsp").forward() dont work with JSP?

I try to redirect my page to another using request.getRequestDispatcher("/index.jsp").forward(request, response); . But it dont work. Why? But when I change it to response.sendRedirect it work fine.
I think the problem might be because of not using relative url.
You can try like this
request.getRequestDispatcher("index.jsp").forward(request, response);
I think you do need the forward slash with the JSP filename.
This is just a small possibility (need more info) - but do you have an init() method in your servlet?
If you do, you must call super.init(servletConfig) as the first line of your init() method, or you might get a NullPointerException when you try to forward.

calling servlet from action class

I am calling a servlet from an action class by using forward. It is then going to the servlet but it is not showing the output.
Actually I have create a PDF file which I need to show it on runtime. If I run that servlet only on server then it is showing the PDF file I want to.
But if I forward it from the action class it is not not showing anything.
I have given a simple condiiton on the action class like this:
if(id.equals("SGSY"))
{
forward = mapping.findForward("SGSY");
}
else
{
forward = mapping.findForward("fail");
}
After this it is going to servlet but not actually showing the output. I don't understand why. Am I doing something wrong?
Try response.sendRedirect 1st instead of forward mapping,if it works it means there must be problem with your path

Categories

Resources