【问题标题】:string search with two parameters带两个参数的字符串搜索
【发布时间】:2012-02-13 08:00:36
【问题描述】:

我需要创建一个简单的应用程序,其中 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>&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>

【问题讨论】:

  • 这段代码有什么问题?运行时会发生什么?
  • 我没有得到输出。因为它应该是“数据成功输入数据库”或“失败”。如果该语句为真,则会打开 result.jsp 页面,其中显示“数据已成功输入数据库”。当我输入新的 Voucher 号码和 NIC 号码时,此页面不会打开。
  • 这是我点击提交按钮时的结果
  • 错误 404--Not Found From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1: 10.4.5 404 Not Found 服务器没有找到任何匹配请求 URI 的东西。没有说明这种情况是暂时的还是永久性的。如果服务器不希望向客户端提供此信息,则可以使用状态代码 403(禁止)来代替。如果服务器通过一些内部可配置的机制知道旧资源永久不可用并且没有转发地址,则应该使用 410(Gone)状态代码。
  • 您将 servlet 映射到 /promotionservlet,但 JSP'a 表单操作指向 PromotionServlet。注意大小写的区别。

标签: jsp servlets web.xml


【解决方案1】:

您似乎将结果打印到标准输出流而不是将其发送到结果页面。使用类似

response.setAttribute("resultMessage","Data successfully entered to the database");

并在结果 jsp 上检索该响应属性并将其打印出来,例如

c:out var="${resultMessage}"/&gt;

(或者直接在页面中使用变量)

【讨论】:

  • 我如何用 DWR 做同样的事情。
  • 我不熟悉 DWR,但我认为您需要在 DWR 中提供 resultMessage 以便它可以远程它。
  • 谢谢 Vasquez.. 我试试这个。
  • 嗨,它终于工作了。谢谢瓦斯奎兹。我将结果作为request.setAttribute("resultMessage","数据成功进入数据库");并将结果 jsp 上的响应属性检索为 它工作正常。
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 2014-06-03
  • 2015-12-28
  • 1970-01-01
  • 2011-01-30
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多