Saving a file with PrintWriter in Tomcat 6 and Windows

Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

Saving a file with PrintWriter in Tomcat 6 and Windows

Post by darknkreepy3# »

If you have your tomcat setup properly, you can access it on port 8080 by default, or 80, even 8088 if you have your apache server on 8080 to allow verizon fios to have apache access (port forward any to 8080 and any to 8081 on the computer with apache's intranet ip, 192.168.1.3 and router 192.168.1.1 etc).

Then if apache is on 8080 instead of 80, then tomcat can run on 8088. port forward any to 8088 and remember to change the tomcat file for port hosting, tomcat6=server.xml

Code: Select all

    <Connector port="8088" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
now, the example I have made into a zip had both the folder Printfile (the servlet) and a .war file of it to be put in the main 'webapps' folder. Tomcat automatically senses this and expands the compressed java file contents into files and folders.
to access it, if you have your tomcat setup correctly, you can goto any browser and type:

Code: Select all

http://127.0.0.1:8088/Printfile/test.html
So tomcat knows to goto the Printfile folder and (servlet) then it runs test.html which has a FORM with POST variables that are sent via the servlet request in the ACTION. remember it's not a page you're going to, it's a servlet that has a java compiled program into a class you are accessing and sending information to.

the program then reads the sent variables and then finds the absolute path your servlet is on to teach you about pathing, and saves the file into the "webapps/Printfile/Printfile_output.html" location.

Code: Select all

String uPath=getServletConfig().getServletContext().getRealPath("")+File.separator+"Printfile_output.html";
never store files you create inside of WEB-INF or any subfolder, that is reserved for the inner workings of tomcat, and is invisible anyway to anyone going through your site or tomcat server via a web browser anyway.


Here is a zip file of the servlet and java compiled to a class. Remember if you want to edit or use this, install JDK from the web (google JDK for winx86 or x86 whichever you have), set your paths correctly, have tomcat installed, make sure all of the debug otions in web.xml are uncommented properly for learning and not a normal commercial deployment. Also make sure all of your PATHS and such for JAVA_HOME and CATALINA_HOME are properly setup (google it)

http://www.supercala.net/tuts/tomcat/Printfile.zip
Post Reply