简体   繁体   中英

Passing value from servlet to Textbox in JSP

I'm trying to develop a simple web app that takes the input and run a specific command, and return the result to the user. I'm struggling a little bit when I'm trying to pass the result/output to a textbox, it always shows null... What am I missing here?

JAVA Code:

public boolean CheckSite(String site) throws Exception
    {
        try 
        { 
        Process p=Runtime.getRuntime().exec("cmd /c nslookup -debug "+site+".abc.internal.rpz | findstr 666"); 
        p.waitFor(); 
        BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
        String line=reader.readLine(); 
        if (line == null)
        {
            System.out.println("This website is not blocked");
            return false;
        }
        else if(line!=null && line.contains("666"))
        { 
            System.out.println("Website is blocked");
            return true;

        } 

        } 
        catch(IOException e1) {}
        return false; 
    }

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        String site = request.getParameter("text1");
        try {
            b=ec.CheckSite(site);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        request.setAttribute("value", b);
    }

JSP Page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Website Status</title>
</head>
<body>
<form align ="middle" action ="checkBlocked" method="post">
Please Enter The Website
<input type ="text" name ="inputText" >
<br>
<input type ="submit" value ="Submit">
</form>
<input type="text" name="done" value='<%=request.getAttribute("TextValue")%>'/> 

</body>
</html>

Thank you in advance

You're setting attribute name:

  request.setAttribute("value", b);

And trying to retrieve as:

  getAttribute("TextValue")

Scriptlets are not recommended. JSP EL and JSTL are the way to go for this task (IMHO).

Make your servlet name is checkBlocked like it says below:

<form align ="middle" action ="checkBlocked" method="post">

Then forward or redirect the response from your servlet to your jsp:

request.getRequestDispatcher("confirmationPage.jsp").forward(request, response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM