I have tried using servlet , but it is not working.
web.xml :
<display-name>Password</display-name>
<servlet>
<servlet-name>SetPassword</servlet-name>
<servlet-class>com.cx.view.SetPassword</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SetPassword</servlet-name>
<url-pattern>/view/setPassword</url-pattern>
</servlet-mapping>
I have a HTML page in [email_templates/setPassword.html]
java code :
public class SetPassword extends HttpServlet {
private static final long serialVersionUID = 7514856921920494774L;
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("email_templates/setPassword.html");
requestDispatcher.forward(request, response);
}
//tried for both get and post request
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("email_templates/setPassword.html");
requestDispatcher.forward(request, response);
}
}
Url trying to access: http://localhost:8080/view/setPassword
Spring boot serves automatically static resources from one of these directories:
/META-INF/resources/
/resources/
/static/
/public/
So try to save your page in one of these folders (I would suggest /static/ or /public/)
Related
So i got this problem, i do a servlet project using tomcat and I got this class that should handle the exception that was thrown and then display the jsp with error status code.
public class ErrorHandler extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(ErrorHandler.class);
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
Integer statusCode = (Integer) req.getAttribute("javax.servlet.error.status_code");
if (throwable != null) {
LOGGER.fatal("Exception: " + throwable);
}
if (statusCode != null) {
LOGGER.fatal("Error, status code: " + statusCode);
}
req.getRequestDispatcher("error.jsp").forward(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
This handler is mapped in the web.xml this way.
<servlet-mapping>
<servlet-name>ErrorHandler</servlet-name>
<url-pattern>/error</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error</location>
</error-page>
The question is, why do i keep getting default tomcat exception page, instead of mine. My logger works just fine, so it's clear, that after exception was happened, method doGet is called, but when it comes to request dispatcher, it just doesn't work. My jsp pages are placed in webapp folder, near WEB-INF.
Because the request dispatcher for error.jsp is just a dispatcher. Java/tomat isn't going to realize that error.jsp means you intended for this to be the error handler. It's just a page, just like foo.jsp. So, that dispatcher picks up the call, so to speak, notices that the request involves an error state, and it, in turn, forwards the request to the error dispatcher.
I have simple servlet that prints something to response
#WebServlet(name = "helloServlet", value = "/hello-servlet" , s )
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Hello World!";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
// Hello
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>" + message + "</h1>");
out.println("</body></html>");
}
public void destroy() {
}
}
that what web.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>hello-servlet</welcome-file>
</welcome-file-list>
</web-app>
and the problem is that url isn`t changed when it redirect to servlet in welcome-file list
my url : http://localhost:8080/testsss_war_exploded/
but should be : http://localhost:8080/testsss_war_exploded/hello-servlet
That is the way welcome resources are supposed to work:
The container may send the request to the welcome resource with a forward, a redirect, or a container specific mechanism that is indistinguishable from a direct request.
(Servlet 5.0 Specification)
Tomcat redirects the request internally. If you want to send a HTTP redirect, you need to do it yourself. You can check the original URI to see if the request was forwarded by the welcome files mechanism:
#WebServlet(name = "helloServlet", urlPatterns = {"/hello-servlet"})
public class HelloServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getRequestURI().endsWith("/")) {
resp.sendRedirect("hello-servlet");
return;
}
}
Another solution would be to renounce the the welcome files mechanism and explicitly bind your servlet to the applications root:
#WebServlet(name = "helloServlet", urlPatterns = {"", "/hello-servlet"})
public class HelloServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getServletPath().isEmpty()) {
resp.sendRedirect("hello-servlet");
return;
}
}
I am new to servlet concept. My requirement is like converting restful given URL into query parameter in the body.
Given URL :
http://anydomain:8080/ServletBasics/HelloForm/India/Andhrapradesh
Required Output URL:
http://anydomain:8080/ServletBasics/HelloForm?Country=India&State=Andhrapradesh
URL fetching has been done by using given servlet code. Could anybody help me out to convert given URL into query based URL. Thanks
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
String vid = request.getRequestURI();
out.println("</body></html>");
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
modified code: sdfd.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String url = request.getRequestURI();
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
if(url.equals("/servletTest/v1/code")) {
String[] words = url.split("/");
String newURI = url.replace(url, "/ws/simple/Apicode?"+"first_name="+words[2]+"&"+"last_name="+words[3]);
RequestDispatcher rd = request.getRequestDispatcher(newURI);
rd.forward(request, response);
out.println(newURI);
}
else
{
out.println("bad");
}
out.println("</html>");
out.println("</body>");
out.close();
}
web.xml
<servlet>
<servlet-name>sdfd</servlet-name>
<servlet-class>sdfd</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sdfd</servlet-name>
<url-pattern>/v1/code</url-pattern>
</servlet-mapping>
I am trying to convert
http://localhost:8080/servletTest/v1/code
to
http://localhost:8080/servletTest/ws/simple/Apicode?first_name=v1&last_name=code
but i am getting below error.
HTTP Status 404 - /servletTest/ws/simple/Apicode
type Status report
message /servletTest/ws/simple/Apicode
description The requested resource is not available.
Apache Tomcat/7.0.42
Kindly help me where exactly i am going wrong?
thanks
use URLRewrite
You can find the documentation in following url : http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html
for example :
<rule>
<from>^/HelloForm/([a-z]+)/([a-z]+)$</from>
<to>/HelloForm?Country=$1&State=$2</to>
</rule>
To configure UrlRewrite, read manual http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html
A JSP page named Test.jsp is mapped to the following Servlet.
#WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
public final class TestServlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//request.getRequestDispatcher("/WEB-INF/admin_side/Test.jsp").forward(request, response);
response.sendRedirect("TestServlet");
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
}
This Servlet is mapped to a JSP page Test.jsp. The doGet() method is invoked, when a URL like http://localhost:8080/Assignment/TestServlet is entered in the address bar.
The request can be forwarded to the given URL as commented out. Is it possible to redirect to the same JSP page, Test.jsp?
If an attempt is made to do so, Google Chrome complains,
This webpage has a redirect loop
It can however, redirect to other pages under WEB-INF/admin_side.
The POST-REDIRECT-GET pattern works like so: a client sends a POST request, your server handles it and responds with a redirect, ie. a response with a 302 status code and Location header to the appropriate URI. The client makes a GET request to that URI.
Currently, your server is redirecting on both GET and POSTS requests. What's worse is that your GET is redirecting to the same URI that it is handling, creating the redirect loop you are seeing.
Change your Servlet implementation so that the POST sends a redirect, but the GET actually serves up a normal 200 response with HTML, AJAX, etc.
I'm using the channel API in Java runtime. The servlet I have mapped to /_ah/channel/connected does not appear to be running. I am creating a channel, passing the token, and opening it on the server. This works fine. I do see the call to /_ah/channel/connected in my log, however no log messages appear and the code does not appear to be running. Below is my code and web.xml
ChannelConnectedServlet.java:
public class ChannelConnectedServlet extends HttpServlet{
private static final Logger logger = Logger.getLogger(ChannelConnectedServlet.class
.getName());
private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException {
logger.log(Level.WARNING,"test");
//do stuff here
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
logger.log(Level.WARNING,"Channel connected!");
process(req, resp);
}
}
web.xml:
<servlet-mapping>
<servlet-name>ChannelConnected</servlet-name>
<url-pattern>/_ah/channel/connected</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ChannelConnected</servlet-name>
<servlet-class>com.myapp.server.channel.ChannelConnectedServlet</servlet-class>
</servlet>
The same behavior happens with the disconnect request. HELP!!!
This entry in web.xml should have included "/" at the end of the url, such as:
<servlet-mapping>
<servlet-name>ChannelConnected</servlet-name>
<url-pattern>/_ah/channel/connected/</url-pattern>
Works now.