First, I am a newbie in Java/J2EE development. So, please be indulgent with my leak of vocabulary (but feel free to correct me ;)
Here is my first problem :
I built a first form (named form1) in a .jsp page :
<form name="form1" action="formaction1.do" method="get">
I redirect the result of my form to a FormAction1 java class :
<servlet>
<servlet-class>com.servlet.myproject.FormAction1</servlet-class>
<servlet-name>FormAction1</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>FormAction1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Here is my FormAction1 java class :
public class FormAction1 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
request.getRequestDispatcher("formaction1.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
This is working fine.
Now, I'd like to create another form, named form2, and link it to FormAction2.
However, FormAction1 receives every .do request !
I tried to customize my <url-pattern> by writing :
<url-pattern>formaction1.do</url-pattern>
I guess it would've been too easy :D
Tomcat doesn't like it : I get a 404 error on every page of my project.
So, do you have any solution ?
Just a bonus question :
What's the point to use a class like FormAction1, rewrite doGet method, while I can just write :
<form name="form1" action="anotherFile.jsp" method="get">
and recover infos with a request.getParameter() in anotherFile.jsp ?
doesn't url-pattern requires a match starting from the beginning, in that case
<url-pattern>/formaction1.do</url-pattern>
Related
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 2 years ago.
I created a dynamic web project in Eclipse with a HTML page (in the WebContent folder of the project) that is supposed to send some input data from the user to a servlet (with the name "Tee").
I try to locate the servlet with
form method="get" action="../../src/Tee"
it does not find it. No, its not a Status 404 error message. But simply "the page cannot be displayed."
I tried this:
form method="get" action="/Tee"
as well, does not work either.
The Tomcat server is started and the project is deployed on the server. If i start the servlet itself, it runs on the server without problem (with all the data set to null, as these are supposed to come from the html page).
Yes, there are similary questions out there but those gave no real solution to me.
You need to write a servlet class something like:
#WebServlet("/Tee")
public class Tee extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.write("Hello World!");
//...
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//...
}
}
After writing the servlet, right-click its name in Eclipse and click Run As > Run on server
If it runs successfully in the Eclipse browser, it will be accessible in your HTML with its name e.g.
<form method="GET" action="Tee">
<input type="submit">
</form>
Update
After you posted the servlet code, I can see a problem there. You have written method="GET" in the HTML form but in the servlet code, you have just doPost. You should have doGet in the servlet for method="GET" in the HTML form to be acted on. Add the method, doGet in addition to doPost in the servlet.
I have a simple web application that runs on Tomcat.
The first page opened after I run the app from IntelliJ is the page from index.html
There user click on button and is redirected to dashboard page. It looks like this in index.html:
<button id="enter" type="button"> ENTER </button>
I have a servlet:
#WebServlet("/dashboard")
public class DashboardServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("html/dashboard.html");
rd.forward(request, response);
}
}
And I have dashboard.html file located in web/html/dashboard.html
After user clicks on button on first page, he is redirected to the next page (the dashboard page) but I see only 404 instead of page.
I cannot find what is wrong here. To solve the problem I added this to my web.xml:
<servlet>
<servlet-name>DashboardServlet</servlet-name>
<servlet-class>airport.flight.control.servlet.DashboardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DashboardServlet</servlet-name>
<url-pattern>/dashboard</url-pattern>
</servlet-mapping>
It gave me is error message from Tomcat when I opened the problematic dashboard page again:
java.lang.ClassNotFoundException: airport.flight.control.servlet.DashboardServlet
I find this exception very weird, because this is the reference to class that I've just copied...
I am trying to refresh just the starting page (calling some functions) say "1.jsp" and as soon as the condition is fulfilled, it is redirected to another jsp page,say "2.jsp", but not sure why the 2.jsp is also getting refreshed. Not only that, the function which was called in 1.jsp is also getting called.Below is sample code just for understanding:
1.jsp
<body>
<h1>Hello World!</h1>
<%
// Here i am trying to read some txt file which is contantly being updated. (by refreshing the page)
// when txt file is written completely, some character like "### DONE ###" will be present at its last line.
// once "### DONE ###" is found , it will be redirected to "2.jsp"
System.out.println("1");
// if "###DONE###" found
RequestDispatcher rd=request.getRequestDispatcher("2.jsp");
rd.forward(request, response);
%>
</body>
2.jsp
<body>
<h1>Hello World! PAGE 2</h1>
</body>
web.xml
<welcome-file-list>
<welcome-file>1.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>SampleFilter</filter-name>
<filter-class>com.SampleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SampleFilter</filter-name>
<url-pattern>/2.jsp</url-pattern>
</filter-mapping>
** SampleFilter**
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) {
public void setHeader(String name, String value) {
System.out.println(name+"-------------------------------");
if (!name.equalsIgnoreCase("Refresh")) {
System.out.println("inside");
super.setHeader("Refresh", "2");
}
}
});
}
My guess is that when you perform rd.forward(request, response) you actually forward the response.addHeader("Refresh","2") with it, causing the next page to refresh automatically as well.
I'm not sure why exactly you need the refresh before the forward, but if you must have it - check if you can remove the specific header ("Refresh"), maybe using the solution proposed here: How do delete a HTTP response header?
EDIT:
Also, instead of triggering the refresh via server-side properties like the header, you should consider doing so via an HTML tag: <meta http-equiv="refresh" content="2" />
In a servlet I would just do
#WebServlet("/myURL")
But how would I do that with a JSP page?
Just like any servlet, you can map a particular URL-pattern to a JSP.
Simply add this snippet in your deployment descriptor
<servlet>
<servlet-name>fooBar</servlet-name>
<jsp-file>/foo.jsp</jsp-file> <!-- Your JSP. Must begin with '/' -->
</servlet>
<servlet-mapping>
<servlet-name>fooBar</servlet-name>
<url-pattern>/bar</url-pattern> <!-- Any URL you want here -->
</servlet-mapping>
There is no facility to have annotations inside the JSP so if you don't want to make an entry inside the web.xml and work purely with annotations, you have a work around to make a sevlet that simply forwards the RequestDispatcher to the JSP and you can annotate this servlet with the URL that you want.
#WebServlet("/bar") //your URL pattern
public class DummyServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/path/to/foo.jsp").forward(request, response);
}
}
I have two jsp file and one java file. My constraints is if jspfile1 call java then java file call the jspfile2. Is it possible?
How to achieve this?
If by "Java file" you mean a Servlet, you can use the RequestDispatcher:
request.getRequestDispatcher("/my.jsp").include(request, response);
request.getRequestDispatcher("/my.jsp").forward(request, response);
The normal way is using a Servlet. Just extend HttpServlet and map it in web.xml with a certain url-pattern. Then just have the HTML links or forms in your JSP to point to an URL which matches the servlet's url-pattern.
E.g. page1.jsp:
<form action="servletUrl">
<input type"submit">
</form>
or
click here
The <form> without method attribute (which defaults to method="get") and the <a> links will call servlet's doGet() method.
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Do your Java code thing here.
String message = "hello";
request.setAttribute("message", message); // Will be available in ${message}.
// And then forward the request to a JSP file.
request.getRequestDispatcher("page2.jsp").forward(request, response);
}
}
If you have a <form method="post">, you'll have to replace doGet by doPost method.
Map this servlet in web.xml as follows:
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/servletUrl</url-pattern>
</servlet-mapping>
so that it's available by http://example.com/contextname/servletUrl. The <form> and <a> URL's have to point either relatively or absolutely to exact that URL to get the servlet invoked.
Now, this servlet example has set some "result" as request attribute with the name "message" and forwards the request to page2.jsp. To display the result in page2.jsp just do access ${message}:
<p>Servlet result was: ${message}</p>
Do a http web request.
jsp files get converted to a servlet. You cannot call them directly.
EDIT : typo fixed.