在JSP中将值从servlet传递到Textbox

时间:2015-12-30 14:56:37

标签: java jsp servlets

我正在尝试开发一个简单的Web应用程序,它接受输入并运行特定命令,并将结果返回给用户。当我试图将结果/输出传递给文本框时,我有点挣扎,它总是显示为空... 我在这里错过了什么?

JAVA代码:

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

提前谢谢

1 个答案:

答案 0 :(得分:1)

您正在设置属性名称:

  request.setAttribute("value", b);

尝试检索:

  getAttribute("TextValue")

不建议使用Scriptlet。 JSP EL和JSTL是完成此任务的方式(恕我直言)。

使您的servlet名称为checkBlocked,如下所示:

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

然后将响应从servlet转发或重定向到jsp:

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