搜索框servlet

时间:2015-09-01 11:47:00

标签: java jsp java-ee servlets

我有index.jsp,我想搜索视频,但每当我搜索它时会给我不同的错误,我无法得到结果,因为它应该将用户带到搜索文本所在的页面。

  

Search.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

  /**
  * Servlet implementation class Search
 */
@WebServlet("/Search")
public class Search extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public Search() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/HTML");
    PrintWriter out = response.getWriter();
    String search=request.getParameter("search");
    try
    {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "MEHAK", "1234");

        PreparedStatement ps=conn.prepareStatement("select * from UPLOADED_FILES where filename=?");  
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            response.sendRedirect("Anime.jsp");
        }
        else{
            out.print("Sorry! Could not recognise the search!");
        }
    }

    catch(Exception e)

    {
        out.println(e.getMessage());
    }
}
}
  

的index.jsp

    <body class="page1">
<%
String u="", Login="Log In";
try{
    u=session.getAttribute("username").toString();
    if(u==null){
        Login="Log In";
    }
    else{
        Login="Log Out";
    }
}
catch(Exception e){

}
%>
    <div class="extra">
<!--==============================banner================================-->
    <header>
        <div class="main">
            <div class="bg-1">
                <div class="box">
                    <h1><a href="index.jsp">Verve</a></h1> 
                    <div class="container-1">
                        <form action="Search" method="post">
                            <span class="icon"><i class="fa fa-search"></i></span>
                            <input type="search" id="search" name="search" placeholder="Search"/>
                        </form>
                    </div>
                </div>
            </div>

这只是index.jsp的一部分,其中实现了搜索框。

1 个答案:

答案 0 :(得分:0)

如果您在?中定义了一个占位符PreparedStatement,则必须设置它:

PreparedStatement ps=conn.prepareStatement("select * from UPLOADED_FILES where filename=?");

将其更改为:

PreparedStatement ps=conn.prepareStatement("select * from UPLOADED_FILES where filename=?");
ps.setString(1,"myFile.txt");