在实际应用中遇到一个问题,把从网上看到的代码复制到本地进行运行,但网上代码有很多数字标号,需要去掉,
package com.homeblog.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SessionServlet extends HttpServlet {
private static final long serialVersionUID = -2352337186490910243L;
/**
*
*/
public void doGet (HttpServletRequest req, HttpServletResponse res)
48: throws ServletException, IOException
49: {
50: //Get the session object
51: HttpSession session = req.getSession(true);
52:
53: // set content type and other response header fields first
54: res.setContentType("text/html");
55:
56: // then write the data of the response
57: PrintWriter out = res.getWriter();
58:
59: out.println("<HEAD><TITLE> " + "SessionServlet Output " +
60: "</TITLE></HEAD><BODY>");
61: out.println("<h1> SessionServlet Output </h1>");
62:
63: // Retrieve the count value from the session
64: Integer ival = (Integer) session.getAttribute("sessiontest.counter");
65:
66: if (ival==null)
67: ival = new Integer(1);
68: else
69: ival = new Integer(ival.intValue() + 1);
70:
71: session.setAttribute("sessiontest.counter", ival);
72:
73: out.println("You have hit this page <b>" + ival + "</b> times.<p>");
74: out.println("Click <a href=" + res.encodeURL("session") +
75: ">here</a>");
76: out.println(" to ensure that session tracking is working even " +
77: "if cookies aren't supported.<br>");
78: out.println("Note that by default URL rewriting is not enabled " +
79: "due to it's expensive overhead");
80: out.println("<p>");
81:
82: out.println("<h3>Request and Session Data:</h3>");
83: out.println("Session ID in Request: " +
84: req.getRequestedSessionId());
85: out.println("<br>Session ID in Request from Cookie: " +
86: req.isRequestedSessionIdFromCookie());
87: out.println("<br>Session ID in Request from URL: " +
88: req.isRequestedSessionIdFromURL());
89: out.println("<br>Valid Session ID: " +
90: req.isRequestedSessionIdValid());
91: out.println("<h3>Session Data:</h3>");
92: out.println("New Session: " + session.isNew());
93: out.println("<br>Session ID: " + session.getId());
94: out.println("<br>Creation Time: " + session.getCreationTime());
95: out.println("<br>Last Accessed Time: " +
96: session.getLastAccessedTime());
97:
98: out.println("</BODY>");
99: }
100:
101: public String getServletInfo() {
102: return "A simple servlet";
103: }
就像这样,需要把标号和:去掉,写了一个类来去除
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 文件操作类:去除文件中的特殊字符
* @author jiangzhiqiang
*/
public class FileEdit {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D://work//hblog//src//com//homeblog//servlet//SessionServlet.java");
BufferedReader br = new BufferedReader(fr);
String s;
StringBuilder sb = new StringBuilder();
while((s=br.readLine())!=null){
for(int i=48;i<104;i++){
String st = i + ":";
if(s.contains(st)){
s = s.replace(st, "");
}
}
sb.append(s);
sb.append("\r");
}
br.close();
System.out.println(sb.toString());
FileWriter fw = new FileWriter("D://work//hblog//src//com//homeblog//servlet//SessionServlet.java");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(sb.toString());
bw.flush();
bw.close();
}
}
这个类可以把从48到103之间的数字+:的组合符号去掉。 如果是其他的数字范围,就修改成相应的数字范围就行。