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.
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 want to set up a cron job that sends an email every 2 minutes. However, when I initiate the cron job, it sends an email right away and then never again. But, when I go to the Google Cloud Console and look at my cron jobs, it says it's running successfully, but I'm not receiving emails.
I followed this tutorial: https://rominirani.com/episode-9-using-the-cron-service-to-run-scheduled-tasks-8bc7dba91a77
web.xml file:
<servlet>
<servlet-name>subscribe</servlet-name>
<servlet-class>blogapp.CronServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>subscribe</servlet-name>
<url-pattern>/subscribe</url-pattern>
</servlet-mapping>
cron.xml file:
<cronentries>
<cron>
<url>/subscribe</url>
<description>Daily Digest from The Rambling Programmer</description>
<!-- <schedule>every day 17:00</schedule> -->
<schedule>every 2 minutes</schedule>
<timezone>America/Chicago</timezone>
</cron>
</cronentries>
CronServlet.java file:
public class CronServlet extends HttpServlet {
private static final Logger _logger = Logger.getLogger(CronServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
_logger.info("Cron Job has been executed");
/// other logic to send email
/// sendEmail(email, subject, content);
}
resp.sendRedirect("/subscribe.jsp");
}
catch (Exception ex) {
resp.getWriter().println("Error subscribing");
}
}
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
There's no errors popping up and one email successfully sends so I'm not sure why it's not running every two minutes like I wanted it to.
Thank you!
I found out it's because it wasn't calling sendEmail() for some reason!
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/)
To avoid re-developing the wheel. Are there any example Java EE servlet filters that take care of some basic security checks/ i.e.
Block web requests for a time period if a rootkit hits the server, ie with a url that ends in .exe or contains "../../.."
Throttle or block IP's that are making unexpectedly high number of requests.
I also wonder if something equivalent to a Thread.sleep(1000); in the servlet filter for those particular types of requests wouldn't be such a bad thing.
Maybe this will help.
public class SuspiciousURLFilter implements Filter {
#Override
public void destroy() {
}
#Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();
if (requestURI.endsWith(".exe")) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
//send error or maybe redirect to some error page
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
filterChain.doFilter(request, response);
}
#Override
public void init(FilterConfig config) throws ServletException {
}
}
In your web.xml:
<filter>
<filter-name>suspiciousURLFilter </filter-name>
<filter-class>your.package.SuspiciousURLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SuspiciousURLFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This question already has answers here:
HTTP Status 405 - HTTP method is not supported by this URL
(2 answers)
Closed 9 years ago.
public class RoarHistoryUpdate extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
super.doGet(request, response);
System.out.println("do Get");
response.setContentType("text/html");
response.getOutputStream().print("Success");
}
}
This is my Servlet. And it is registerd in the web.xml like this:
<servlet>
<display-name>RoarHistoryUpdateServlet</display-name>
<servlet-name>RoarHistoryUpdateServlet</servlet-name>
<servlet-class>de.ulm.uni.vs.avid.roary.servlets.RoarHistoryUpdate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RoarHistoryUpdateServlet</servlet-name>
<url-pattern>/Roary/UpdateServlet</url-pattern>
</servlet-mapping>
When I go to the URL http://localhost:8080/Roary-JSP/Roary/UpdateServlet It says HTTP Status 405 - HTTP method GET is not supported by this URL
Funny thing is I get the do Get logged to my console. So it actually found the doGet-method.
I am using a GlassFish Server Open Source Edition 3.1.2.2
Because when you do super.doGet(request, response); in your Servlet's doGet() method, you actually call the doget() of the HttpServlet class. The Tomcat 7 implementation of that method is as below (may be a similar implementation exists for Glassfish):
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
My guess is that it's because of invoking super.doGet(). If you check the source code of HttpServlet, you'll see that it sets this status code on the response. So drop the super call. It's not needed.