access WEB-INF/jsp from html - java

I have a simple application to test the communication between html and jsp. My jsp is located in
WEB-INF/test.jsp
Here is the structure of my files:
ProjectA
src
irstServlet.java
Web-Content
test1.html
WEB-INF
test.jsp
Here is the code from servlet
protected void doPost(HttpServletRequest request, response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test.jsp");
request.setAttribute("userName", request.getParameter("userName"););
dispatcher.forward(request, response);
}
First I have deploy in tomcat start my test1.html: It take me to the servlet: FirstServlet.java and I can enter userName there.
But after i enter the values in and press enter I expect it to forward me to test.jsp which is not working. I get the error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Edited:
In my html I am trying to use it like:
<form method="POST" name="XX" action="/HelloWorldServlet">
Still not working.
Please can someone help me?

Your code does not look like it would compile at all.
Parameter response has no type - should be HttpServletResponse
There is a semicolon (;) after request.getParameter("userName")
Also I'm not sure why you're getting RequestDispatcher from servlet context rather than from the request - then again I've never checked if it makes any difference.
Anyway, I would rewrite doPost method like this:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("userName", request.getParameter("userName"));
req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req, resp);
}
EDIT:
I'm assuimng you have either a correct servlet mapping in your web.xml:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
or your servlet is annotated with #WebServlet annotation:
#WebServlet("/HelloWorldServlet")
public class FirstServlet extends HttpServlet {
//your code
}
. If neither of those is true, that's your problem right there.

Related

Folder hierarchy java web

I think that my folder hierarchy is wrong.
Should the .html and .jsp file is in WEB-INF?
I tried to move them there, but I received an error 404.
No, everything you put in the WEB-INF folder will NOT be available to the users of your web application. Your hierarchy looks OK.
WEB-INF resources not directly visible for the public. You can use webservlet annotation. For example;
#WebServlet(urlPatterns = {"/adminlogin", "/register"})
or servlet mapping(old-fashioned),
web.xml :
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>yourPackage.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/adminlogin/</url-pattern>
<url-pattern>/logout/</url-pattern>
<url-pattern>/register/</url-pattern>
<url-pattern>/userlogin/</url-pattern>
</servlet-mapping>
LoginController
//post get method
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
if (userPath.equals("/logout")) {
//Your Model
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
} ...
}

Http status 404 -/the requested resource is not available [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
I am new to java, I just tried to read initialization parameters from Deployment Descriptor file (web.xml), But got above error?
My web.xml and java file coding coding in snap attached.
My directrory structure is
c:\....tomcat\webapps\dd\web-inf\classes
No error in java class file.
Java file code which is compiled successfully
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet {
String fileName;
public void init(ServletConfig config) throws ServletException {
super.init(config);
fileName = config.getInitParameter("logfilename");
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request,HttpServletResponse
response)throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(fileName);
out.close();
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>MyServlet2</servlet-name>
<servlet-class>MyServlet2</servlet-class>
<init-param>
<param-name>logfilename</param-name>
<param-value>value1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet2</servlet-name>
<url-pattern>/mc11</url-pattern>
</servlet-mapping>
</web-app>
Other detail of my directory and error page i think that my web.xml not working
There are two problems I can see at the moment...
Servlet init parameters
You currently have:
//defining param1
param1
value1
That's not how you define the parameter. You should specify a param-name element containing the name of the parameter, rather than using the name of the parameter as an XML element name.
<init-param>
<param-name>logfilename</param-name>
<param-value>...</param-value>
</init-param>
Also note that // isn't how you write comments in XML - if you wanted a comment, you should have:
<!-- Define the first parameter -->
<init-param>
<param-name>logfilename</param-name>
<param-value>...</param-value>
</init-param>
(The param-value element should have been a hint - if you could really just specify your own element, I'd have expected <logfilename>value in here</logfilename> - having the name specified as an element name, but the value specified with a fixed element name of param-value would have been an odd scheme.)
Servlet mapping
Currently your mapping is:
<servlet-name> FormServlet</servlet-name>
<url-pattern>/ss/</url-pattern>
</servlet-mapping>
I suspect that mapping won't match http://localhost:8080/dd/ss/s.html because you don't have any wildcards in there - it you may well find that it matches exactly http://localhost:8080/dd/ss/. It's not clear where the dd part comes in, but I assume that's a separate part of your configuration. You should try:
<!-- I would recommend removing the space from the servlet
- name *everywhere*. -->
<servlet-name>FormServlet</servlet-name>
<url-pattern>/ss/*</url-pattern>
</servlet-mapping>
If that doesn't work for http://localhost:8080/dd/ss/s.html, see whether it maps http://localhost:8080/ss/s.html - it may be that your engine isn't configured the way you expect elsewhere.
There is no problem with code,I try net beans tool and done assignment perfectly with the help of above code.Before that may be problem in tomcat.

Not Understanding Servlet RequestDispatcher

I have one doubt in Servlet RequstDispatcher. it has 2 methods(include and forward) right. my doubt is what happen when we use forward method following by include method..
RequestDispatcher rd = req.getRequestDispatcher("/S2");
rd.forward(req, res);
rd.include(req, res);
in this case these 2 methods executing fine or not ..
Thanks in Advance,
public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException
Includes the content or data of a resource (servlet, JSP page, or HTML file) in the response.
More information on : http://www.javatpoint.com/requestdispatcher-in-servlet

Google App Engine JSP

I have created a Google App Engine project, but because of some SEO concerns I want to change one of my pages from HTML (+ JQuery) to a JSP that gets rendered on the server
This page is the index.html file, how can I make it work as a JSP without renaming it (I don't want the user to go to index.jsp, but instead treat index.html as a JSP page)
I've tried adding this to my web.xml, but it doesn't seem to work
<servlet>
<servlet-name>main</servlet-name>
<jsp-file>/index.html</jsp-file> (or index.html, same result)
</servlet>
Any ideas on how to solve this ?
If I rename the index.html to index.jsp file, everything works fine
You can definitely do this in a Servlet filter.
Set up your filter to catch requests to /index.html
Then in the filter return index.jsp so it is seen by the client as /index.html
ex:
private ServletContext context;
#Override public void init(FilterConfig arg0) throws ServletException {
context = arg0.getServletContext();
}
#Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
context.getRequestDispatcher("/index.jsp").include(request, response);
}
What this does is include /index.jsp in the response. Of course, since you don't have a /index.html file then that ends up being the whole response.

JSF default tomcat error page

I want to display the default Tomcat error page in JSF 2.0 (MyFaces) application when exception is thrown.
I added following lines to web.xml:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/ErrorHandler</location>
</error-page>
<context-param>
<param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
<param-value>false</param-value>
</context-param>
And here is ErrorHandler servlet:
public class ErrorHandler extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Error");
}
}
But instead of the default Tomcat error page an empty page is displayed with error code 500.
When I try to access ErrorHandler servlet directly through URL, it works OK: Tomcat error page is displayed.
So I guess the reason is JSF error handling mechanism? What am I doing wrong?
If you want to display the default error page of the app server, just remove the error page entry from your web.xml and set the context init-parameter javax.faces.PROJECT_STAGE to Production.

Categories

Resources