Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7496111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:29:14+00:00 2026-05-29T18:29:14+00:00

I need to create a simple application where the Servlet reads two inputted parameters,

  • 0

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>&nbsp;</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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:29:17+00:00Added an answer on May 29, 2026 at 6:29 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes you need to create a very simple single file application in Qt4. However
I need to create a simple document assembly application to create Word files. We
I need to create a very simple application: I need to have a form
I need to create a simple installer of sorts for a different application. That
I need to create a simple c# application to add some quarterly figures. I
I'm developing a simple application for Android devices. I need to create a directory,
I need to code a small/simple database application using C, for my CS degree
I need to create a simple application that is multi-platform that will send data
I've created a simple Flex application to fetch an XML file. I need a
I need to create simple reusable javascript object publishing several methods and parameterized constructor.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.