动态向文本框添加值

发布于 2024-11-05 17:00:05 字数 8484 浏览 0 评论 0原文

Jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <script type="text/javascript" language="Javascript" >

        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add Course_Subject</title>
<style type="text/css">
            <!--
body {
    background-color: #FFCCFF;
}
.style1 {
    color: #0066FF;
    font-weight: bold;
}
.style2 {font-size: 18px}
.style17 {  font-family: "Monotype Corsiva";
    font-size: 24px;
    font-weight: bold;
    font-style: italic;
    color: #6633CC;
}
.style19 {color: #000099}
.style21 {color: #000099; font-weight: bold; }
-->
</style>


    </head>
    <body>
        <jsp:include page="Log_Admin.jsp"/><br/>
        <form action="" method="post" name="form1" id="form1">
        <table width="46%" height="43" border="3" bgcolor="##CCCC99" align="center">
            <tr>
                <td width="85%" align="center" bgcolor="#FFFF99"><label><span class="style17">Course and Subject Information</span></label></td>
            </tr>

            <tr><td>
                <table width="666" height="207" border="0" align="center" bordercolor="#F0F0F0" bgcolor="#CCCC99" >
                    <tr>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Coourse ID</strong></label>
                        </span></div></td>
                        <td><label>
                            <select name="cid" size="1" id="cid" align="left" onclick="loaadCourseName()">
                            <option selected="selected">None</option>
                            <option>C001</option>
                            <option>C002</option>
                            <option>C003</option>
                            <option>C004</option>
                            <option>C005</option>
                            <option>C006</option>
                            <option>C007</option>
                            <option>C008</option>
                            <option>C009</option>
                            <option>C010</option>
                            </select>
                        </label></td>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Coourse Name</strong></label>
                        </span></div></td>
                        <td width="310" align="left"><input name="cname" type="text" id="cname" size="25" maxlength="50" /></td>
                    </tr>

                    <tr>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Subject ID</strong></label>
                        </span></div></td>
                        <td><label>
                            <select name="sid" size="1" id="sid" align="left">
                            <option selected="selected">None</option>
                            <option>S01</option>
                            <option>S02</option>
                            <option>S03</option>
                            <option>S04</option>
                            <option>S05</option>
                            <option>S06</option>
                            <option>S07</option>
                            <option>S08</option>
                            <option>S09</option>
                            <option>S10</option>
                            </select>
                        </label></td>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Subject Name</strong></label>
                        </span></div></td>
                        <td width="310" align="left"><input name="sname" type="text" id="sname" size="25" maxlength="50" /></td>
                    </tr>

                    <tr>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name="save" type="submit" id="save" value="Save" onclick="validate(this.form)"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input name="reset" type="reset" id="reset" value="Reset" /></td>
                    </tr>
                </table>
            </td></tr>
        </table>
    </form>
    </body>
</html>

Servlet

package DBCon;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
 *
 * @author Nayan
 */
public class searchCourseName extends HttpServlet {

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String cname=null,courseid;
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost/online_exam?"+"user=root&password=pass");
            Statement stmt=con.createStatement();

            courseid=request.getParameter("cid");
            ResultSet rs=stmt.executeQuery("select course_name from course where course_id='"+courseid+"'");

            while(rs.next())
            {
                cname=rs.getNString("course_name");
                //String s=rs.getString(1);
            }
            request.getSession().setAttribute("courseName",cname);
            //RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse2.jsp");
            //requestDispatcher.forward(request,response);

        }
         catch(Exception e) { 
            out.println("<h1>"+e.getStackTrace()+"</h1>");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

现在,当我在下拉列表 cid 中选择一个项目时,我想在 cname 文本字段中显示相应的 courname。我怎样才能实现这个目标?

Jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <script type="text/javascript" language="Javascript" >

        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add Course_Subject</title>
<style type="text/css">
            <!--
body {
    background-color: #FFCCFF;
}
.style1 {
    color: #0066FF;
    font-weight: bold;
}
.style2 {font-size: 18px}
.style17 {  font-family: "Monotype Corsiva";
    font-size: 24px;
    font-weight: bold;
    font-style: italic;
    color: #6633CC;
}
.style19 {color: #000099}
.style21 {color: #000099; font-weight: bold; }
-->
</style>


    </head>
    <body>
        <jsp:include page="Log_Admin.jsp"/><br/>
        <form action="" method="post" name="form1" id="form1">
        <table width="46%" height="43" border="3" bgcolor="##CCCC99" align="center">
            <tr>
                <td width="85%" align="center" bgcolor="#FFFF99"><label><span class="style17">Course and Subject Information</span></label></td>
            </tr>

            <tr><td>
                <table width="666" height="207" border="0" align="center" bordercolor="#F0F0F0" bgcolor="#CCCC99" >
                    <tr>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Coourse ID</strong></label>
                        </span></div></td>
                        <td><label>
                            <select name="cid" size="1" id="cid" align="left" onclick="loaadCourseName()">
                            <option selected="selected">None</option>
                            <option>C001</option>
                            <option>C002</option>
                            <option>C003</option>
                            <option>C004</option>
                            <option>C005</option>
                            <option>C006</option>
                            <option>C007</option>
                            <option>C008</option>
                            <option>C009</option>
                            <option>C010</option>
                            </select>
                        </label></td>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Coourse Name</strong></label>
                        </span></div></td>
                        <td width="310" align="left"><input name="cname" type="text" id="cname" size="25" maxlength="50" /></td>
                    </tr>

                    <tr>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Subject ID</strong></label>
                        </span></div></td>
                        <td><label>
                            <select name="sid" size="1" id="sid" align="left">
                            <option selected="selected">None</option>
                            <option>S01</option>
                            <option>S02</option>
                            <option>S03</option>
                            <option>S04</option>
                            <option>S05</option>
                            <option>S06</option>
                            <option>S07</option>
                            <option>S08</option>
                            <option>S09</option>
                            <option>S10</option>
                            </select>
                        </label></td>
                        <td width="186" height="46" align="left"><div align="left"><span class="style19">
                        <label><strong>Subject Name</strong></label>
                        </span></div></td>
                        <td width="310" align="left"><input name="sname" type="text" id="sname" size="25" maxlength="50" /></td>
                    </tr>

                    <tr>
                        <td>      </td>
                        <td>       <input name="save" type="submit" id="save" value="Save" onclick="validate(this.form)"/>                
                        <input name="reset" type="reset" id="reset" value="Reset" /></td>
                    </tr>
                </table>
            </td></tr>
        </table>
    </form>
    </body>
</html>

Servlet

package DBCon;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
 *
 * @author Nayan
 */
public class searchCourseName extends HttpServlet {

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String cname=null,courseid;
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost/online_exam?"+"user=root&password=pass");
            Statement stmt=con.createStatement();

            courseid=request.getParameter("cid");
            ResultSet rs=stmt.executeQuery("select course_name from course where course_id='"+courseid+"'");

            while(rs.next())
            {
                cname=rs.getNString("course_name");
                //String s=rs.getString(1);
            }
            request.getSession().setAttribute("courseName",cname);
            //RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse2.jsp");
            //requestDispatcher.forward(request,response);

        }
         catch(Exception e) { 
            out.println("<h1>"+e.getStackTrace()+"</h1>");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

Now when I select an item in the dropdown cid I want to display the corresponding courname in the cname textfield. How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-11-12 17:00:05

删除

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

使用 JSP 时它们是多余且危险的。 JSP 已经设置了自己的内容类型。当在 servlet 中获取响应编写器时,转发到 JSP 时,您只会面临在服务器日志中看到 IllegalStateException 错误的风险。


替换

request.getSession().setAttribute("courseName",cname);
//RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse2.jsp");
//requestDispatcher.forward(request,response);

by

request.setAttribute("courseName",cname);
request.getRequestDispatcher("/removeCourse2.jsp").forward(request, response);

这会在请求范围内设置变量,以便它可通过 ${courseName} 使用,并将请求转发回表单所在的同一 JSP。会话范围将也可以工作,但你不希望在这里这样做。它将影响其他请求(例如,访问者可能在多个浏览器选项卡中打开了相同的表单)。


更新

<input name="cname" type="text" id="cname" size="25" maxlength="50" />

$

<input name="cname" value="${courseName}" type="text" id="cname" size="25" maxlength="50" />

{courseName} 将打印请求属性的值。在输入元素的 value 属性中执行此操作将使其显示在浏览器中。如果这是用户控制的值,您可能需要使用 JSTL fn:escapeXml() 来避免 XSS 攻击。


另请参阅:

Remove

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

They are superfluous and dangerous when using JSP. JSP already sets its own content type. When getting the response writer in the servlet, you'll only risk seeing IllegalStateException errors in server logs when forwarding to JSP.


Replace

request.getSession().setAttribute("courseName",cname);
//RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse2.jsp");
//requestDispatcher.forward(request,response);

by

request.setAttribute("courseName",cname);
request.getRequestDispatcher("/removeCourse2.jsp").forward(request, response);

This sets the variable in request scope so that it's available by ${courseName} and forwards the request back to same JSP where the form is in. The session scope will also work, but you don't want that here. It'll affect other requests (the visitor may for instance have opened the same form in multiple browser tabs).


Update

<input name="cname" type="text" id="cname" size="25" maxlength="50" />

with

<input name="cname" value="${courseName}" type="text" id="cname" size="25" maxlength="50" />

The ${courseName} will print the value of the request attribute. Doing so in the value attribute of the input element will make it to show up in the browser. If this is an user controlled value, you may want to use JSTL fn:escapeXml() to avoid XSS attacks.


See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文