Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have HttpServletRequest req and HttpServletResponse res wont to use req and res variables in another place in code.
and I have another problem in if statement when I was pass function in compression

What I have tried:

@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 
        {
       
         
         }
//       
    }
   
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900