我正在使用 Java Servlet。在那里,我必须使用正则表达式来过滤掉文本。但是,java 编译器在 import java.util.regex.Pattern 的导入处说;“无法解析符号”
因此,我所有的连续匹配器和模式功能都不起作用。
我们不能在 servlet 中使用正则表达式包吗?我无法使用字符串的 contians 函数,因为它也给了我一个错误。有什么替代品吗?
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.List;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class HelloWorldExample extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException
{
response.setContentType("text/plain;charset=ISO-8859-1");
PrintWriter out = response.getWriter();
String keyword = request.getParameter("inputValue");
HttpClient cl = new HttpClient();
String temp = "http://localhost:8080/solr/select/?q=*%3A*&version=2.2&start=0&rows=2000&indent=on";
PostMethod pst = new PostMethod(temp);
String XMLstr="<GIS>";
Perl5Util perl=new Perl5Util();
try{
cl.executeMethod(pst);
String xmlstr=pst.getResponseBodyAsString();
SAXBuilder sax=new SAXBuilder();
Document doc= (Document) sax.build(new StringReader(xmlstr));
Element rootElem=doc.getRootElement();
Element res=rootElem.getChild("result");
List docs=res.getChildren("doc");
for(int i=0;i<docs.size();i++)
{
out.println("docsSize = " + docs.size());
Element row= (Element)docs.get(i);
List strs=row.getChildren("str");
out.println("strs = " + strs.size());
out.println("Hello");
Element strRow=(Element)strs.get(0);
String strContent=strRow.getText().toString();
Element strRow1=(Element)strs.get(1);
String Lati=strRow.getText().toString();
Element strRow2=(Element)strs.get(2);
String Longi=strRow.getText().toString();
Element strRow3=(Element)strs.get(3);
String FileName=strRow.getText().toString();
if(strContent.contains(keyword))
{
//out.println("XMLstr: ");
XMLstr+="<File><id>"+FileName+"</id><Lat>"+Longi+"</Lat><Long>"+F+"</Long>";
//out.println("XMLstr: "+XMLstr);
}
//out.println(strRow.getText());
break;
}
}catch(Exception e)
{
out.println(e);
}
}
}