I've a URL pattern like webroot/TellSomeoneMail and corresponding class,
<servlet>
<servlet-name>TellSomeoneMail</servlet-name>
<display-name>Tell Someone Mail</display-name>
<servlet-class>com.nightingale.register.servlet.TellSomeoneMailServlet</servlet-class>
</servlet>
but how to identify which JSP file calling this servlet?
You can identify during execution into our servlet by looking to the referer header in the HTTP body:
String referrer = request.getHeader("referer");
Edit 1: You can also use session to keep the last url acceded by the user (such mechanism is already present in framework like grails or Spring under the "flash" attribute, not to be confused with adobe flash). If you use simple Servlet / JSP, you need to code such support...
Edit 2 Last solution if cookie and referee is blocked, is to add a parameter in the URL with reference to the last page, for instance URL?from=home_pg or URL?from=/homepage.html but it could require rewriting of urls embedded in the page.
you can get the URL from which the request was sent. Take a look at the following code
if (request instanceof HttpServletRequest) {
String url = ((HttpServletRequest)request).getRequestURL().toString();
}
To find JSP pages that allow the user to make requests to your servlet : Check the path the servlet is mapped under in the <servlet-mapping> element(s) in web.xml.
Then do a full text search on all the JSP's in your projet for this string. Look for HTML <a> and <form> element with target containing your servlet path.
Related
I have a search.jsp page that has some html content and a form. When the form is submitted, there is a servlet handle the form data and forward the results to the search.jsp page. However, the url in the browser after processing the form is changed to the servlet name:
http://localhost:8080/MyProject/SearchServlet
not the search.jsp page:
http://localhost:8080/MyProject/Search.jsp
How I can change the url to the search.jsp? In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?
You cannot do that by forwarding the request: you need to "tell" the browser to generate a new http request by using the response.sendRedirect() method.
Now the question is why do you want the url bar to display the name of the Jsp?
Hiding the real destination path is a desired feature when forwarding requests: users do not need to know the server side redirects (that's how they are also called) happening in your web app.
Think about it: to carry out its tasks a servlet potentially can forward the request a number of times before getting to the final destination: you don't want the url bar to change each and every single time.
Give a fancier name to your servlet like: "Search" rather than "SearchServlet" so that users will know they are on the search page of your web application and not in the "SearchServlet" page.
In addition to that, if you visit any professional website, you will hardly ever see the .jsp or .html or .php extension on the address bar. While that is not a requirement or specification and you are free to do so, I believe the first approach is best practice (it looks even better to me honestly). There is even a folder WEB-INF whose purpose is to hide your .jsp pages from direct access via url bar.
What I like doing is having a servlet as the landing-welcome page of the web app, that will be responsible to forward and redirect requests based on the user input and the inner working of the application.
Now back to your final request (pun intended)
"In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?"
What I would do is:
redirect the user to the "Search" servlet from the welcome/home servlet.
In the doGet method of the Search servlet I would forward the request to the search.jsp page (you could set attributes before forwarding if you need to).
In the search.jsp I would set the action attribute of the form to "Search" (the name of the servlet) and the method to POST.
In the doPost method of the Search servlet you would implement whatever logic you wish to implement and finally forward the request to the search.jsp
After hitting the search button (and even after the submit button is clicked) what the user will see on the address bar is simply
http://localhost:8080/MyProject/Search
Hope that makes sense.
Are you using the same search.jsp for searching and well as showing the result? It is possible to use the same jsp to perform both the functions but it's easier and desirable to make another jsp which will only show the results.
If you are not able to see the results on search.jsp then make sure that you are setting the proper response in the Servlet class before forwarding it to the jsp and also whether you are reading the response sent by the Servlet class properly in the jsp.
If you want, the page to not refresh at all, then go for AJAX.
How to show response or output generated by servlet using separate .html file designed with CSS?
For example, output generated by servletresp.java in htmlpage.html.
Can we use CSS in Servlet programming?
You question is very generic so I'm going to assume you are a beginning Java web programmer.
To your first question, I advise you to use JSP pages (or any other template technology).
Yes, it is possible to serve .html files from a Servlet using a RequestDispatcher, but JSP pages are meant to generate such output - it is easy to make a small part of a JSP page dynamic, while serving HTML files from a servlet doesn't give you an option for some dynamic behaviour.
Just rename your file.html page to file.jsp and put in in your web source directory of your war project.
To your second question - HTML sent by a servlet or a JSP page is still normal HTML and you can use CSS as you would in any HTML page.
Code example:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/file.html");
// If you want to include the file in your response, use dispatcher.include -
// you can include multiple different files or send more output using
// response.getWriter() or response.getOutputStream
dispatcher.include(request, response);
// If you just want to send this one file as the response, use dispatcher.forward
dispatcher.forward(request, response);
}
If your aim is to have the user's browser (appear to) request a static .html page, but have your servlet code executed instead, there's a few ways you could do that.
Most (All? I'm only familiar with running Struts on WebSphere) servlet containers allow you to specify an arbitrary pattern for URLs that are mapped to actions, so you could simply put in a mapping for htmlpage.html to your servlet action that executes the associated code then runs through a JSP (or similar) to render HTML to be sent as the response.
Alternatively you could simply serve up a static HTML page that uses a JavaScript (so executed client-side) templating engine, then load the data for the page from your servlet (likely as JSON) via an AJAX call when the page loads.
The second part doesn't really make any sense. CSS isn't a programming language, it's simply rules for specifying how things look. It's also meaningless without something to interpret and apply those rules to the content (which is what the browser does). You can include CSS inside HTML, or a separate file, generated in your servlet code, but it's not going to do anything.
I am using a session scope to store the bean,and i want to project the bean value to the jsp page when needed like this way
request.getSession().setAttribute("bean", bean);
response.sendRedirect("test.jsp");
And in the jsp i am using the below code to get the value on jsp
<% bean1 bean = (bean1) session.getAttribute("bean");
%>
<%= bean.getValue() %>
Instead of using a session scope i want to use a request scope,so can i set my attribute in my servlet in this way
request.setAttribute("bean", bean);
So how can i call it on my jsp
can i say
<% bean1 bean = (bean1) request.getAttribute("bean");
But it is showing error.Or instead of using scriplet how can i show my output using JSTL.
You're not understanding what a redirect is. A redirect is a response you send to the browser so that the browser sends another, new request to the location you redirected to. So, when you call sendRedirect("test.jsp"), the browser will send a new request to test.jsp. And obviously, all the attributes you have stored in the current request won't be available anymore.
It's impossible, without context, to say if a redirect is something you should do in this case, or if you should instead forward to the JSP. A forward is very different from a redirect, since it only transfers the responsibility of the current request and response to another component. In that case, there would be a unique request, and the JSP could find the attribute set by the servlet in the request.
The only thing I can say is that, in a properly designed MVC application, the JSP is used as a view, and there should never be a direct request to the view. Each request should go through a controller.
i have this scenario. User enter some stuff on jsp form in browser and submit. In servlet i process the request and show the jsp page1 to client which has just continue
button. Now on click of continue, i want to forward this request to another jsp page2 with all request parameter present on page1. basically i want to get all request parameters which were present in first request on page 2 also? I am not getting how should i go
for this? I dont think i can use jsp forward as per my understanding it would work only when both when we want to forward from jsp (on server side) not on client side?
There are two ways to implement that:
output all parameters in hidden fields, and submit them in the 2nd request
store everything in the session
Following are a pair of ways to achieve this
Put the information in session object in first request and access it from session object in second.
On the intermediate page displayed, have hidden form elements which will carry the values and send it back on continuing.
You can use Servlet Forward to pass all request parameters to another JSP or Servlet:
getServletContext().getRequestDispatcher("/yourJSP.jsp").forward(request, response);
Servlet forward will forward the existing request to another JSP or Servlet, so all the request parameters and attributes will be available to the destination JSP or Servlet (The reason you can use the Servlet forward is that JSP is also a Servlet after translation).
You may find following resources useful:
Pass data from servlet to jsp
Servlet Redirect vs Forward
I'm using netbeans to create a web application that allows a cell phone user to scan a QR code and retrieve a journal's previous/next title information. The take home point is that there's no form that the user will input data into. The QR code will contain the search string at the end of the URL and I want the search to start on page load and output a page with the search results. For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. Oh, to top it off...I'm brand new to programming in java and using netbeans. I have no clue about what javabeans are, or any of the more advance techniques. So, with that background explanation here's my question.
I have created a java class (main method) that parses my xml and retrieves the results correctly. I have an index.jsp with my html in it. Within the index.jsp, I have no problem using get to get the title information from my URL. But I have no clue how I would get those parameters to a servlet that contains my java code. If I manage to get the search string to the servlet, then I have no idea how to send that data back to the index.jsp to display in the browser.
So far every example I've seen has assumed you're getting form data, but I need parameters found, processed and returned on page load...IOW, with no user input.
Thanks for any advice.
Ceci
Just put the necessary preprocessing code in doGet() method of the servlet:
String foo = request.getParameter("foo");
// ...
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
And call the URL of the servlet instead of the JSP one e.g. http://example.com/page?foo=bar instead of http://example.com/page.jsp?foo=bar in combination with a servlet mapping on an URL pattern of /page/*.
You can get the url parameter in servlet using request.getParameter("paramName");
and you can pass the attribute to page from servlet using forwarding request from servlet to jsp.
See Also
Servlet wiki page
Bear in mind that your JSP page will compile internally to a servlet. So you can retrieve the string and print it back in the same JSP. For instance assuming you get the string in a parameter called param you would have something like this in your JSP:
<%
String param = request.getParameter( "param" );
out.println( "String passed in was " + param );
%>
One last thing, you mentioned the main method -- that only gets executed for stand alone applications -- whereas you're talking web/JSP :O