开发者

displaying data from database in to text box

I have 2 JSP pages as below:

projectcategory.jsp

 <%
    Connection con = DbConnect.connect();
    Statement s = con.createStatement();

    ResultSet rs = s.executeQuery("select * from projectcategory");
 %>
        <DIV class="TabbedPanelsContent" align="center">
        <TABLE border="1">
            <TR>
                <TH>CATEGORY ID</TH>
                <TH>CATEGORY NAME</TH>
                <TH>Edit/Update</TH>
            </TR>

            <%
                while (rs.next()) {
            %>
            <%String p=rs.getString(1);%>

            <TR>
                <TD><%=rs.getString(1)%></TD>
                <TD><%=rs.getString(2)%></TD>
                <TD>

                <FORM action="EditPcat.jsp?pcatid=p"><INPUT type="submit"
                    value='edit/update'></INPUT>
                </FORM>

                </TD>
            </TR>

            <%
                }
            %>
        </TABLE>
        </DIV>

another is Editpcat.jsp:

</head>
<body>

<%String s=request.getParameter("p"); %>



<form action="ProjCatServlet" method="post">
<div align="right"><a href="projectcategory.jsp">view</a></div>
<fieldset>

<legend>Edit category</legend>
<table cellspacing="2" cellpadding="2" border="0">
    <tr>
        <td align="left">Category Id</td>

        <td><input type="text" name="pcatid" value="<%=s%>" ></td>
    </tr>

    <tr>
        <td align="right">Category Name</td>
        <td><input type="text" name="pcatname"></td>
    &开发者_如何学Golt;/tr>

    <tr>
        <td><input type="submit" value="submit"></td>
    </tr>

</table>

<input type="hidden" name="FUNCTION_ID" value="UPDATE">

</fieldset>
</form>

How to display value from one JSP page which we get from database in to text box of another JSP?


You're passing it as a request parameter with the name pcatid:

<FORM action="EditPcat.jsp?pcatid=p">

But you're trying to obtain it as a request parameter with the name p:

<%String s=request.getParameter("p"); %>

Align the names out.

That said, you tagged this question with servlets, but you aren't using servlets at all. This code is honestly said a disaster. Scriptlets are a poor practice and the JDBC code is leaking resources. Raw Java code belongs in Java classes, not in JSP files. Use taglibs like JSTL to control the page flow and use EL (Expression Language) to access backend data. Go through those tutorials to get it right.

Kickoff example:

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<Project> projects = projectDAO.list();
    request.setAttribute("projects", projects);
    request.getRequestDispatcher("projects.jsp").forward(request, response);
}

and

<table>
    <c:forEach items="${projects}" var="project">
        <tr>
            <td>${project.id}</td>
            <td>${project.name}</td>
        </tr>
    </c:forEach>
</table>


This is very dirty but you can add hidden INPUT tags in your form:

<INPUT type="hidden" name="p1" value="<%=rs.getString(1)%>">
<INPUT type="hidden" name="p2" value="<%=rs.getString(2)%>">

BTW: you should close your statement when no longer needed, otherwise you may use too much resources on the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜