Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpServletRequest-req和HttpServletResponse-res不会使用req和res变量_Java_Servlets - Fatal编程技术网

Java HttpServletRequest-req和HttpServletResponse-res不会使用req和res变量

Java HttpServletRequest-req和HttpServletResponse-res不会使用req和res变量,java,servlets,Java,Servlets,我有HttpServletRequest req和HttpServletResponse res习惯在代码中的另一个位置使用req和res变量。 当我在压缩中传递函数时,if语句中还有另一个问题 这是我的密码 @WebServlet(name = "NaiveBayesExample", urlPatterns = {"/NaiveBayesExample"}) public class NaiveBayesExample extends HttpServlet { String pa

我有HttpServletRequest req和HttpServletResponse res习惯在代码中的另一个位置使用req和res变量。 当我在压缩中传递函数时,if语句中还有另一个问题

这是我的密码

@WebServlet(name = "NaiveBayesExample", urlPatterns = {"/NaiveBayesExample"})
public class NaiveBayesExample extends HttpServlet {
     String param="";

    public static String[] readLines(URL url) throws IOException {

        Reader fileReader = new InputStreamReader(url.openStream(), Charset.forName("UTF-8"));
        List<String> lines;
        try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            lines = new ArrayList<>();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines.toArray(new String[lines.size()]);
    }


      public void doPost(HttpServletRequest req,HttpServletResponse res) throws   IOException           
    { 
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();        
        param =req.getParameter("a");       
        pw.print("<br> <font color=blue size=5>POST METHOD</font>");
        pw.print("Param is "+ param);

    }  
      protected void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException           
    {  
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();        
        param =req.getParameter("a");       
        pw.print("Param is "+ param);
    }

  public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {

        PrintWriter out = res.getWriter();
        res.setContentType("text/plain");
        String paramName = "param name";
        String paramValue = req.getParameter(paramName);
        out.write(paramName + " = ");
        out.write(paramValue);
        paramName = "UNKNOWN";

        paramValue = req.getParameter(paramName);
 if (paramValue==null) {
            out.write("Parameter " + paramName + " not found");
        }
         out.close();
    }
    public static void main(String[] args) throws IOException {

        //map of dataset files
        Map<String, URL> trainingFiles = new HashMap<>();
        trainingFiles.put("Paaass Request", NaiveBayesExample.class.getResource("/datasets/training.normaltraffic.nt.txt"));
        trainingFiles.put("Sql Injectionnn Request", NaiveBayesExample.class.getResource("/datasets/training.sqlinjection.si.txt"));

        //loading examples in memory
        Map<String, String[]> trainingExamples = new HashMap<>();
        for(Map.Entry<String, URL> entry : trainingFiles.entrySet()) {
            trainingExamples.put(entry.getKey(), readLines(entry.getValue()));
        }
        //train classifier
        NaiveBayes nb = new NaiveBayes();
        nb.setChisquareCriticalValue(6.63); //0.01 pvalue
        nb.train(trainingExamples);

        //get trained classifier knowledgeBase
        NaiveBayesKnowledgeBase knowledgeBase = nb.getKnowledgeBase();

        nb = null;
        trainingExamples = null;

        //Use classifier
        nb = new NaiveBayes(knowledgeBase);

//        String PassTraffic = "http://www.testsite.com/catigories/index.php=1";

        String output = nb.predict(req.getParameter("a"));
        if (output!=trainingFiles.put("Pass", NaiveBayesExample.class.getResource("/datasets/training.normaltraffic.nt.txt")))   
        { 
             res.sendRedirect("SecondServlet"); 
        }
        else 
        {

        }

//        System.out.format("The Traffic \"%s\" was classified as \"%s\".%n", PassTraffic, outputpass);
//       

        String output2 = nb.predict(req.getParameter("a"));
         if (output2!=trainingFiles.put("stop", NaiveBayesExample.class.getResource("/datasets/training.sqlinjection.si.txt")))

         {
              res.sendRedirect("SecondServlet"); 
        }
        else 
        {
@WebServlet(name=“NaiveBayesExample”,urlPatterns={”/NaiveBayesExample})
公共类NaiveBayeSample扩展了HttpServlet{
字符串参数=”;
公共静态字符串[]读线(URL)引发IOException{
Reader fileReader=新的InputStreamReader(url.openStream(),Charset.forName(“UTF-8”);
列出行;
try(BufferedReader BufferedReader=新的BufferedReader(文件阅读器)){
行=新的ArrayList();
弦线;
而((line=bufferedReader.readLine())!=null){
行。添加(行);
}
}
返回lines.toArray(新字符串[lines.size()]);
}
public void doPost(HttpServletRequest-req、HttpServletResponse-res)引发IOException
{ 
res.setContentType(“文本/html”);
PrintWriter pw=res.getWriter();
param=req.getParameter(“a”);
打印(
POST方法); 打印(“参数为”+参数); } 受保护的void doGet(HttpServletRequest-req、HttpServletResponse-res)引发IOException { res.setContentType(“文本/html”); PrintWriter pw=res.getWriter(); param=req.getParameter(“a”); 打印(“参数为”+参数); } public void handleRequest(HttpServletRequest-req,HttpServletResponse-res)引发IOException{ PrintWriter out=res.getWriter(); res.setContentType(“文本/普通”); 字符串paramName=“param name”; 字符串paramValue=req.getParameter(paramName); out.write(参数名+“=”); out.write(参数值); paramName=“未知”; paramValue=req.getParameter(paramName); if(paramValue==null){ out.write(“参数”+paramName+“未找到”); } out.close(); } 公共静态void main(字符串[]args)引发IOException{ //数据集文件的映射 Map trainingFiles=新建HashMap(); trainingFiles.put(“Paaass请求”,NaiveBayesExample.class.getResource(“/datasets/training.normaltraffic.nt.txt”); trainingFiles.put(“sqlinjectionnn请求”,NaiveBayesExample.class.getResource(“/datasets/training.sqlinjection.si.txt”); //在内存中加载示例 Map trainingExamples=newhashmap(); 对于(Map.Entry:trainingFiles.entrySet()){ trainingExamples.put(entry.getKey()、readLines(entry.getValue()); } //列式分级机 NaiveBayes nb=新的NaiveBayes(); 注:setChisquareCriticalValue(6.63);//0.01 pvalue 注意:培训(培训示例); //获取经过培训的分类器知识库 NaiveBayesKnowledgeBase knowledgeBase=nb.getKnowledgeBase(); nb=null; trainingExamples=null; //使用分类器 nb=新的朴素贝叶斯(知识库); //字符串PassTraffic=”http://www.testsite.com/catigories/index.php=1"; 字符串输出=nb.predict(请求getParameter(“a”)); if(output!=trainingFiles.put(“Pass”,NaiveBayesExample.class.getResource(“/datasets/training.normaltraffic.nt.txt”)) { res.sendRedirect(“SecondServlet”); } 其他的 { } //System.out.format(“流量\%s\”被分类为\%s\“%n”,PassTraffic,outputpass); // 字符串output2=nb.predict(请求getParameter(“a”)); if(output2!=trainingFiles.put(“stop”,NaiveBayesExample.class.getResource(“/datasets/training.sqlinjection.si.txt”)) { res.sendRedirect(“SecondServlet”); } 其他的 {
}} }


在if语句中,编译器说了不可比较的类型:String和URL以及res和req不可访问以获取参数

不要将字符串与==进行比较=仅使用equals函数。 您必须将URL强制转换为字符串,然后您只能比较这两个字符串

比如说

 if (output.equals(trainingFiles.put("Pass", NaiveBayesExample.class.getResource("/datasets/training.normaltraffic.nt.txt")).toString())) {
        res.sendRedirect("SecondServlet");
 }

它会起作用的!非常感谢您的支持,但我还有一个小问题,就是req和res无法用于examble res.sendRedirect(“SecondServlet”);在if station中未定义res您必须在函数中定义res,并且在您将使用它的位置进行定义。这不起作用,它们在doPost和doGet中定义,就像这个public void doPost(HttpServletRequest req,HttpServletResponse res)抛出IOException{}Yes,您只能在
doPost
函数中使用这两个变量,而不能在main中使用。您必须创建它们或从其他函数获取它们。