HttpServletRequest req and HttpServletResponse res wont to use req and res variables

时间:2018-03-25 18:54:14

标签: java servlets

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

here my code

@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 
        {

} } }

in if statement compiler said incomparable type: String and URL and res and req not accessible to get parameters

1 个答案:

答案 0 :(得分:0)

Don´t compare strings with == or != only with the equals function. And you must caste the URL to a string, after that you can only compare this two.

For example

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