NebroProg Ответов: 0

Как использовать res httpservletresponse в качестве общей переменной


У меня есть 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 
        {
       
         
         }
//       
    }
   
}

0 Ответов