Why does getRealPath() return null when deployed with a .war file? [duplicate]

For a start, ServletRequest.getRealPath(String path) is deprecated. The appropriate replacement is: ServletContext context = session.getServletContext(); String realContextPath = context.getRealPath(request.getContextPath()); However, the API docs for ServletContext.getRealPath(String path) state: “This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made … Read more

How to get the original request url from a servlet/jsp after multiple servlet forwards

I found a better answer in this post [ How do you detect the URL in a Java Servlet when forwarding to JSP? ] On the target JSP use: request.getAttribute(“javax.servlet.forward.request_uri”) To find out what the original URL was. It doesn’t require you to take any extra steps on the forwarding servlet

How to save data with gson in a json file?

You write all the users in same file C:\\file.json so just the last iteration of the loop saved. You can convert the object List<User> into json and write it once (no needed loop) Example: try (Writer writer = new FileWriter(“Output.json”)) { Gson gson = new GsonBuilder().create(); gson.toJson(users, writer); }

Configure embedded jetty with web.xml?

Use a org.eclipse.jetty.webapp.WebAppContext Example: package jetty; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class OnWebApp { public static void main(String[] args) throws Exception { // Create a basic jetty server object that will listen on port 8080. // Note that if you set this to port 0 then a randomly available port // will be assigned that … Read more

What do you monitor with JMX in your production Java application?

At the JVM level, I monitor the garbage collection duration per minute, At the Servlet Container level, I monitor the number of requests, number of exceptions (4xx & 5xx codes) , sum of request duration per minute, At the SOAP level, I monitor the number of invocations, number of exceptions & sum of invocations per … Read more

What’s your “best practice” for the first Java EE Spring project? [closed]

Small tip – I’ve found it helpful to modularize and clearly label my Spring xml context files based on application concern. Here’s an example for a web app I worked on: MyProject / src / main / resources / spring / datasource.xml – My single data source bean. persistence.xml – My DAOs/Repositories. Depends on datasource.xml … Read more

How to connect with Java into Active Directory

Here is a simple code that authenticate and make an LDAP search usin JNDI on a W2K3 : class TestAD { static DirContext ldapContext; public static void main (String[] args) throws NamingException { try { System.out.println(“Début du test Active Directory”); Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11); ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, “com.sun.jndi.ldap.LdapCtxFactory”); //ldapEnv.put(Context.PROVIDER_URL, “ldap://societe.fr:389”); ldapEnv.put(Context.PROVIDER_URL, “ldap://dom.fr:389”); ldapEnv.put(Context.SECURITY_AUTHENTICATION, “simple”); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)