Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JSP EDITOR QUETION? NEED HELP

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
ok, ive just created a e-commerce web site using jsp.
This was a project I had to do for my final year in college.
I have to put up all the source code on the web so my lecturers can see it.
I want to store my code in HTML file, and I also want thes HTML files to highlight the syntax in different colors, to make it look clear instead of having it in black and white.
Is there any program out there that will let me convert my jsp files into HTML docs to show the code in the HTML doc and highlighted?
Need this to get me more marks for presentation.
All help is welcome.
Thanks
rui
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK - here is a little servlet you can employ....all you do is pass the JSP file reference to it.
This code can be modified to selectively seek the tags you wish to highlight and add the proper CSS class= designation or FONT attributes, etc.
---------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JSPSourceServlet extends HttpServlet {
/**
* Handles a GET request by sending the contents of the resource
* specified by the servlet path to the client.
*
* @param req an HttpServletRequest
* @param res an HttpServletResponse
* @exception IOException
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
// Get the pathInfo
String path = req.getPathInfo();

// Check for invalid characters in the path
if (path.indexOf("..") != -1 | | path.toUpperCase().indexOf("WEB-INF") != -1) {
res.sendError(HttpServletResponse.SC_FORBIDDEN,
"Illegal characters found in path " + path);
return;
}
sendFile(path, res);
}
/**
* Copies the "in" reader to the "out" writer.
*
* @param in a BufferedReader
* @param out a PrintWriter
*/
private void copyFile(BufferedReader in, PrintWriter out)
throws IOException {
int chars;
char[] buf = new char[4096];
while ((chars = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, chars);
out.flush();
}
}

/**
* Sends the contents of a file to the client.
*
* @param pathInfo the file URI
* @param res an HttpServletResponse
* @exception IOException
*/
private void sendFile(String path, HttpServletResponse res) throws IOException {
String fileName = getServletContext().getRealPath(path);
if (fileName == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND,
path + " not found.");
return;
}

File file = new File(fileName);
if (file.isDirectory() | | !file.canRead()) {
res.sendError(HttpServletResponse.SC_FORBIDDEN,
"No read access to " + file.getAbsolutePath());
return;
}
res.setStatus(HttpServletResponse.SC_OK);
// Use "text/html" and convert HTML characters to
// character entities, to fool IE as explained above
res.setContentType("text/html");
long lastMod = file.lastModified();
if (lastMod != 0) {
res.setDateHeader("Last-modified", lastMod);
}
PrintWriter out = res.getWriter();
// Add HTML header
out.println("<html><head><title>" + path + "</title></head>");
out.println("<body><pre>");

// Read the file and convert it
BufferedReader in = new BufferedReader(new FileReader(file));
StringWriter rawText = new StringWriter();
copyFile(in, new PrintWriter(rawText));
in.close();
out.write(toHTMLString(rawText.toString()));

// Add HTML footer
out.println("</pre></body></html>");
out.close();
}
}
// convert special characters to browser-safe equivalents
public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer();
for (int i = 0; in != null && i < in.length(); i++) {
char c = in.charAt(i);
if (c == '\'') {
out.append("'");
}
else if (c == '\"') {
out.append(""");
}
else if (c == '<') {
out.append("<");
}
else if (c == '>') {
out.append(">");
}
else if (c == '&') {
out.append("&");
}
else {
out.append(c);
}
}
return out.toString();
}
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic