I have developed a simple application such as insertion, updation in jsp,servlet that is java web. What i want to do is: suppose i am inserting data into my oracle db then data is inserted and i got a message that data has been inserted successfully, same thing i want to show in a command prompt when my web app is running in tomcat server. Application will run in browser and simultaneously command prompt will show that :
this data is inserted into this table---transaction committed successfully----
How can i implement that? Any help is much appreciated
The vast majority of Java web applications use Log4j for logging.
To log messages to the server's console, use the Log4j ConsoleAppender class.
Normally you need to do little more than drop log4j.jar into your tomcat/lib directory, and configure logging with a log4j.xml or log4j.properties file, then have your servlets or jsps or controllers or services issue logging commands. There are many tutorials on the web that can get you started learning log4j with tomcat.
Oh and by the way, you can try plain old System.out.println() calls from a servlet, though this is only for learning. Professional applications should use a logger.
Related
With a standard webapp running in Tomcat with the Spring Framwork and Log4J logging to catalina.out I need to have a better access to logs than manual SSH and tail -f catina.out
I already know of some solution like logstash, ... but they require to send the log to a centralized server. I went through a lot of answers of various websites but none satisfies my needs. I just want to have access to the logs in a web browser on the same web server.
Is there any simple and straightforward way to do that ?
Update
I want to to that because I cannot always SSH and tail -f the logs because of the firewall IP security. I need to be able to see these logs from anywhere as long as I have an internet access to such a secure live web console.
Give logsniffer a try. It's a simple standalone Java web application which can run on the same host. log4j log format is supported out of the box, just type in the conversion pattern and the logs will be parsed properly. You can tail, search and monitor the logs in real-time. Last but not least, logsniffer is open source.
Disclaimer: This is my own project.
I made a web app with a ServletContextListener that in its contextInitialized() just calls two methods from two jars. The jars use log4j to print logs in its execution, what is useful for me on testing. Now, I package this app in an ear file, and deploy into a WAS, that is located into a server to which I don't have access (meaning, I can't access FTP to see paths on the server, etc) to set the path for the logs in the server, and it will take some time to get actual permission to set this.
I thought that maybe I could set log4j to print the logs in my working machine (that is also connected to the network the server is connected)
Is there anyway to do this?
Log4j has many appenders: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html
For Log to different machine you can use SocketAppender. But for yours situation better store logs in database with JDBCAppender
I have a web application
hosted by tomcat 7.1
using spring 3.1 mvc framework.
I want to implement a command line function for tomcat console window, to read commands and perform some action or output some information to console. How can I do it?
Example:
type "show memory", then display JVM memory information to console.
Can I do it just by System.in? Will there be any thread-safe problem?
If you're running as a web application in Tomcat, you won't get any access to System.in or the console: Tomcat is typically started in the background, detached from all consoles. And it won't provide you witn meaningful access to the console.
The way to go is to either provide a REST API as suggested by #rlegendi in the comments (any other API would work as well) and write a separate command line application that interfaces with your API. Alternatively utilize the "manager" interface - if I remember correctly from ancient past, tomcat's manager application also has some usable methods to access from external scripts.
Make sure to make those calls authenticated - at least validate that they're coming from localhost so that you simulate some kind of security in the API. Don't trust random calls coming in from the world.
I'm working on a beta release of an application. This is a Java app that runs on Glassfish. Obviously we are getting bug reports from time to time. I would like to provide my users with a simple one-click button to get the server log from /domains/domain1/logs . I am not sure if the application is sandboxed in such a way that it can get to this log file.
Does glassfish sandbox the application in such a way that I would be prevented from getting the file? Is there a standard method or library I can use to get the logs?
If the file permissions on the server are correct, can I just have my application read the file using an absolute path?
You application can read the server.log file directly. Note that the logs are rotated, so your application would have to reload the log file and maybe even provide access to the rotated logs.
If you are using a web server front end, such as Apache, you could just serve the log directory. I think you could just create a symbolic link to that directory in your webroot.
Be careful you are not logging any sensitive information as exposing a log file could be a security risk.
If you are using GlassFish 2.1, you should look at AMX. The AMX class Logging seems to have the data that you want.
If you are using GlassFish 3.1, you should use the RESTful interface to the admin data. If you have an instance of GlassFish 3.1 running locally, right now, you can click this link to see the log data: http://localhost:4848/management/domain/view-log...
If you are using a WebKit based
browser, like Chrome or Safari, you
probably need to view the source of
the 'empty' page....
If you are using GlassFish 3.0, you will need to open the file directly.
i am writing a standalone java app. the app's properties should be configurable from a webpage deployed with the app. how do i achieve this ?
Thanks in advance
note: the app has an embedded HTTP client/server module. it should only run from command prompt
I don't think that's a good idea. Webpage forms are designed to work with a server, not with a standalone client app. You could have the app run its own web server, but that would mean the app has to be running for the configuration page to work, and it's also a rather contrived setup just to do some configuration.
It might be possible for the webpage to contain JavaScript that writes to a local file - I don't know enough about the JavaScript security model to say.
But why not have the configuration dialog as part of the app's GUI? That's the normal and expected behaviour - you'd need a pretty compelling reason to deviate from it.
JMX might be the answer that you're looking for. If you expose all of your configurable properties through MBeans, then adding a web page on top of that exposing these properties is just configuration.
You can launch a standalone Java app using JNLP files (Java WebStart). If you want the user to be able to configure the application before its launched, you can have the JNLP file dynamically generated, then pass properties as environment variables through the JNLP file.
You can configure your standalone Java app to read configurable properties from a properties file (say conf.properties) on the server.
You may have a UI webpage (html/jsp) with all the field to be configured. When the page is submitted a JSP/Servlet may write/update the contents of conf.properties on the server.
UPDATE: The above solution will work assuming only an admin user wants to update the properties file.
In case anybody should be able to update it, then concurrency issue has to be taken into account.
In that scenario, you have to implement a mechanism similar to how weblogic10 updates config.xml using Admin Console.
i.e. You will have 2 conf.properties files confA & confB (initially in sync). The standalone app will always read from confB. The UI will have 2 buttons say Lock & Release configurations. When an edit is made (locked & released), it will be written to confA and at the same time changes of confA has to be replicated to confB.