I am learning how to build a Java project in IntelliJ. But I can't make it build automatically, while running the Jetty:run command.
I already set the IDE to autobuild when saving and to build when idle for 10 secs, and set the compiler.automake.allow.when.app.running to 1. All of these methods worked doing in my mac, but in windows it doesn't work.
I have a servlet like so:
package br.com.alura.maven.lojaweb;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet(urlPatterns={"/contato"})
public class ContatoServlet extends HttpServlet{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("<html><h2>My message here!</h2></html>");
writer.close();
}
}
When I change the message and save, I hope to find it changed in the browser
Related
This question already has answers here:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
(7 answers)
Closed 2 years ago.
I am trying to catch OData call in Java with WebServlet.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
#WebServlet("/someservice/UserSet")
public class UserSetServlet extends HttpServlet {
#Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
//some functionality
}
}
When I call simply "http://somdomain.com/someservice/UserSet", then I get to service, but when I call "http://somdomain.com/someservice/UserSet("SOME_ID")", then I receive 404, that service is not found
Does someone know how I can configure Servlet for catching entire OData Request?
Thanks a lot!
Thanks to all answers. Solution - nohow. I starting migration to Spring Framework.
I have a very simple servlet program running normally on google app engine. Now when i add firebase object to make a child value updation call it shows server error 500 on appengine and on local apache tomcat it also shows some error.
Here is my code
package com.example.appengine.helloworld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseException;
#SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,FirebaseException {
PrintWriter out = resp.getWriter();
out.println("Hello, world");
Firebase fire = new Firebase("https://fiery-fire-0.firebaseio.com/");
fire.child("hello").setValue("Nothing is in here");
}
}
here is the error when running it on apache tomcat
Exception in thread "TubeSockReader-1" java.lang.NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier
at com.firebase.tubesock.WebSocket.verifyHost(WebSocket.java:287)
at com.firebase.tubesock.WebSocket.createSocket(WebSocket.java:271)
at com.firebase.tubesock.WebSocket.runReader(WebSocket.java:306)
at com.firebase.tubesock.WebSocket.access$000(WebSocket.java:30)
at com.firebase.tubesock.WebSocket$2.run(WebSocket.java:108)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.ssl.StrictHostnameVerifier
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1854)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
... 6 more
On running it on google app engine it simply says
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.
Kindly help me!
It looks like the version of your http client library doesn't have the (now deprecated) class StrictHostnameVerifier.
https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/ssl/StrictHostnameVerifier.html
Try manually including httpclient 4.0.1 instead of "latest" (although it should appear in 4.5.1 according the docs).
After trying many different url paths I still am not able to map my jsp get request to my servlet. The request keeps returning a 404 resource not found. The jsp and servlet are located in the same /NOSESSION folder. Here is the call from the jsp
<form action="/webapps/xxxx-xxx-BBLEARN/NOSESSION/createLinkReturn_proc" method="get" name="the_form" onsubmit="validateForm();">
And my servlet looks like this
package com.xxxx;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.net.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
#WebServlet("/NOSESSION/createLinkReturn_proc")
public class createLinkReturn_proc extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
LoggingManager.appendToLogFile("Made it into the servlet");
}
I am using Servlet 3.0 with an updated web.xml config. I shouldn't have to put the servlet mappings in the web.xml file, but I have tried that and it still didn't work that way. I have tried lots of combinations of mappings from simple "/createLinkReturn_proc" to "${pageContext.request.servletPath}/createLinkReturn_proc" and nothing has worked so far.
This question already has answers here:
Where is Tomcat Console Output on Windows
(4 answers)
Closed 4 years ago.
I am using Java 7, tomcat 7. While learning about servlet logging I wrote a simple program.
package net.codejava;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LogCheck
*/
#WebServlet("/LogCheck")
public class LogCheck extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public LogCheck() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletContext context = getServletContext();
String par = request.getParameter("msg");
if (par == null || par.equals("")){
context.log("No Message received",new IllegalStateException("Missing Parameter"));
}else{
context.log("Here is visitor's message:"+par);
}
PrintWriter out = response.getWriter();
out.println("Log Testing.");
}
}
Now I am able to see the log messages in my Eclipse console. But I do not see any files under Tomcat Home/logs folder ?
But I read that it should store the log messages in some log files. I do not see any log file in my case.
Am I missing some configuraions? Please help
They will be logged to the file localhost.${today}.log.
As Maks said, ServletContext#log will logged with the category org.apache.catalina.core.ContainerBase. and when you look at Tomcat's logging configuration in conf/logging.properties, you'll see that these messages go to files with the prefix localhost.
https://tomcat.apache.org/tomcat-7.0-doc/logging.html
The calls to javax.servlet.ServletContext.log(...) to write log messages are handled by internal Tomcat logging. Such messages are logged to the category named
org.apache.catalina.core.ContainerBase.[${engine}].[${host}].[${context}]
This logging is performed according to the Tomcat logging configuration. You cannot overwrite it in a web application.
Where the logs live:
When running Tomcat on unixes, the console output is usually redirected to the file named catalina.out. The name is configurable using an environment variable.
When running as a service on Windows, the console output is also caught and redirected, but the file names are different.
I am trying to combine these two sample Java Spring apps together in order to be able to run my Mongo database on one server and serve JSON to a front end app on another server.
CORS tutorial:
http://spring.io/guides/gs/rest-service-cors/
MongoDB with REST
http://spring.io/guides/gs/accessing-mongodb-data-rest/
My current "MongoDB with REST" Spring.io sample application runs, but it currently still isn't supporting CORS even after adding the SimpleCORSFilter class as shown in the CORS sample (above).
I am thinking that I may simply need to configure this new SimpleCORSFilter class somewhere - in the annotation equivalent of the web.xml - so I am trying to use #WebFilter("/*") in the SimpleCORSFilter class.
According to the "MongoDB with REST" tutorial page, the "#Import(RepositoryRestMvcConfiguration.class) annotation imports a collection of Spring MVC controllers, JSON converters, and other beans needed to provide a RESTful front end. These components link up to the Spring Data MongoDB backend." This might indicate that I should also want to override something in RepositoryRestMvcConfiguration to configure the SimpleCORSFilter filter.
I tried adding #WebFilter("/") on the SimpleCORSFilter class itself, but I don't see Access-Control- headers in the response using curl --verbose. As a result, I don't think the filter is properly configured.
Has anyone had any luck with combining these two tutorial projects (or something similar?)?
For reference, the SimpleCORSFilter class I am trying to configure is here:
package foo.bar;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
#WebFilter("/*")
#Component
public class SimpleCORSFilter extends OncePerRequestFilter implements Filter {
/* Attempt #1 - based on original demo code from spring.io - could not get to work
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
*/
// Attempt #2 - based on https://stackoverflow.com/questions/20583814/angular-and-cors which advised use of addHeader() instead of setHeader() - still no luck
#Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS");
response.addHeader("Access-Control-Allow-Headers",
"origin, content-type, accept, x-requested-with, sid, mycustom, smuser");
filterChain.doFilter(request, response);
}
// unable to override this final method
//public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
...and the main Application class which currently seems to be performing the configuration is here:
package foo.bar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
#Configuration
#EnableMongoRepositories
#Import(RepositoryRestMvcConfiguration.class)
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Note: I did find a somewhat similar post here: CORS with Spring not working, but that solution involves the use of JSONP, which I also read was sort of a hack, and in any case, that issue does not seem to be resolved either.
I was running into what I believe is a similar issue. Turning basic authentication off seems to have resolved it.