繁体   English   中英

将值从servlet传递到JSP中的Textbox

[英]Passing value from servlet to Textbox in JSP

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

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>

先感谢您

您正在设置属性名称:

  request.setAttribute("value", b);

并尝试检索为:

  getAttribute("TextValue")

不建议使用脚本。 JSP EL和JSTL是完成此任务(IMHO)的方法。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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