I need to create a simple application where the Servlet reads two inputted parameters, check if either of them in the database the result should prompt “Failed” else “Success”.
Please correct this code.Thank you very much.
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>
You seem to print the result to the stdout stream instead of sending it to the result page. Use something like
response.setAttribute("resultMessage","Data successfully entered to the database");and retrieve that response attribute on the result jsp and print it out, like
c:out var="${resultMessage}"/>(or use the variable directly in the page)