我需要创建一个简单的应用程序,其中Servlet读取两个输入参数,检查数据库中是否有任何一个结果应该提示“失败”,否则“成功”。 请更正此代码。非常感谢。
PromotionServlet.java
package promotion;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PromotionServlet extends HttpServlet {
public PromotionServlet() {
super();
}
private ServletConfig config;
String page = "PromotionResult.jsp";
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = response.getWriter();
/*Establish connection to MySQL database*/
String voucher_num = request.getParameter("voucher_num");
String nic = request.getParameter("nic");
String connectionURL =
"jdbc:mysql://localhost:3306/customer_promotion";
Connection connection = null;
ResultSet rs;
response.setContentType("text/html");
//List dataList=new ArrayList();
String id = "error";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "mobitel#123");
Statement s = connection.createStatement();
rs =s.executeQuery("Select * from promotion_tbl where voucher_num='" + voucher_num + "' OR nic='" + nic + "'");
String voucherNumber = "";
String nicNumber = "";
if (rs.next()) {
rs.getString("voucher_num");
rs.getString("nic");
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
} else {
s.executeUpdate("INSERT INTO promotion_tbl VALUES('" +
voucher_num + "','" + nic + "')");
System.out.println("Data successfully entered to the database");
}
} catch (Exception e) {
System.out.println("Exception is ;" + e);
e.printStackTrace();
}
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null) {
dispatcher.forward(request, response);
}
}
}
的web.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>PromotionServlet</servlet-name>
<servlet-class>promotion.PromotionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PromotionServlet</servlet-name>
<url-pattern>/promotionservlet</url-pattern>
</servlet-mapping>
</web-app>
PromotionJSP.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%-- <%@ page contentType="text/html;charset=windows-1252"%>--%>
<!--<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>PromotionJSP</title>
</head>
<body></body>
</html>-->
<%@ page contentType="text/html;charset=windows-1252"%>
<%@page language="java" import="java.util.*" %>
<html>
<head>
<title>Enter your Voucher Number and NIC number</title>
</head>
<body bgcolor="#999966">
<p> </p>
<form method="GET" action="PromotionServlet">
<p>
<font color="#800000" size="5">
<label for="Voucher_Number:">Enter Your Voucher Number </label></font>
<input type="text" name="voucher_num" size="20"></input>
</p>
<p>
<font color="#800000" size="5">
<label for="NIC_Number:">Enter your NIC Number :</label></font>
<input type="text" name="nic" size="20"></input>
</p>
<p>
<input type="submit" value="Submit"></input>
</p>
</form>
</body>
</html>
答案 0 :(得分:0)
您似乎将结果打印到stdout流,而不是将其发送到结果页面。使用像
这样的东西 response.setAttribute("resultMessage","Data successfully entered to the database");
并在结果jsp上检索该响应属性并将其打印出来,如
c:out var="${resultMessage}"/>
(或直接在页面中使用变量)