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>
}
现在,当我在下拉列表cid
中选择一个项目时,我想在courname
文本字段中显示相应的cname
。我怎样才能做到这一点?
答案 0 :(得分:1)
删除强>
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);
通过
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}
将打印请求属性的值。在input元素的value
属性中执行此操作将使其显示在浏览器中。如果这是用户控制的值,您可能希望使用JSTL fn:escapeXml()
来避免XSS攻击。