如何将包中存在的servlet映射到welcome file-list之外

时间:2016-04-28 10:16:43

标签: java jsp servlets

我对映射servlet感到困惑。实际上我在webcontent中有两个文件index.jsp,在webcontent的“Sales / Ihome.jsp”中有另一个文件Ihome.jsp。当我运行服务器并选择“Sales”时/Ihome.jsp“作为欢迎文件并在文本框中输入”s“然后我能够显示如下所示的数据。enter image description here

但是,当我选择“index.jsp”作为welcome-file,然后单击sales并单击“Ihome.jsp”并在文本框中键入一个字母时,它没有显示上面显示的数据。那么如何映射servlet以便我可以获取数据而不将“Sales / Ihome.jsp”放在welcome-file列表中。我发布下面的代码

的web.xml

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
     <servlet>
    <servlet-name>SearchController</servlet-name>
    <servlet-class>com.controller.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SearchController</servlet-name>
    <url-pattern>/SearchController</url-pattern>
  </servlet-mapping>

Ihome.jsp

<html:html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Invoicing Page</title>

        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">

<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">


    <script type="text/javascript">
$(document).ready(function() {
    $(function() {
        $("#search").autocomplete({

            source : function(request, response) {
                $.ajax({
                    url : "SearchController",
                    type : "POST",
                    data : {
                        term : request.term
                    },
                    dataType : "json",
                    success : function(data) {
                        response(data);

                    }
                });
            },
            select: function( event, ui ) {
                 var name = ui.item.value; 
                 document.location.href ="index1.jsp?name="+name;
                 // Your code
                 return false;
              }
        });
    });
});
</script>
<input type="text" id="search" name="search" />

Controller.java

package com.controller;

import java.io.IOException;
import java.util.ArrayList;

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

import com.model.DataDao;
import com.google.gson.Gson;

public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json");
        try {
            String term = request.getParameter("term");
            System.out.println("Data from ajax call " + term);

            DataDao dataDao = new DataDao();
            ArrayList<String> list = dataDao.getFrameWork(term);

            String searchList = new Gson().toJson(list);
            response.getWriter().write(searchList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

DataDao.java

package com.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class DataDao {
    private Connection connection = null;

    public DataDao() throws Exception {

        Class.forName("com.mysql.jdbc.Driver");

        // set the url, username and password for the databse
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
    }

    public ArrayList<String> getFrameWork(String frameWork) {
        ArrayList<String> list = new ArrayList<String>();
        PreparedStatement ps = null;
        String data;
        try {
            ps = connection
                    .prepareStatement("SELECT * FROM marketing_database.lead  WHERE Company_Name  LIKE ?");
            ps.setString(1, frameWork + "%");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                data = rs.getString("Company_Name");
                list.add(data);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;
    }
}

的index.jsp

<%@ page import="java.io.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.mail.*"%>
<%@ page import="java.util.*"%>
<%@ page import ="javax.activation.*"%>
<%@ page import="java.sql.*"%>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<%@taglib prefix="html" uri="/WEB-INF/struts-html.tld" %>

<html:html>
    <head>
        <title>
            <bean:message key="welcome.title" />
        </title>
        <%
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Expires", "0");//this to take out cache.
            response.setDateHeader("Expires", -1);
                   %>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <link rel="shortcut icon" type="image/x-icon" href="image/favicon.ico">
     <link rel="shortcut icon" type="image/x-icon" href="/SalesPropeller/image/favicon.ico">  </head>
    <body>
        <div id="divContainer">
            <div id="divBanner"><img src="image/Banner-final.JPG" width="1000" height="150"/>
            </div>
            <hr />
            <div id="divBody">
                <div id="divRegister">
                    <img src="image/lead.JPG" /><br/>
                    <img src="image/PURCHASE.jpg"/><br/>
                    <img src="image/sale.jpg"/><br/>
                    <img src="image/customer.jpg"/><br/>
                    <img src="image/attendance.jpg"/><br/>
                    <img src="image/payroll.jpg"/><br/>
                </div>
                <div id="divLogin" style="width: 435px; height: 230px; margin: 130px 550px" >
                    <div id="divText"><bean:message key="form.title.login"/></div>

                          <html:form action="/login" method="post">    
                        <table cellspacing="0px" cellpadding="10px">
                            <tr>
                                <td><bean:message key="form.email"/></td>
                                <td><html:text property="email"/></td>
                                <td><html:errors property="loginemail"/></td>
                            </tr>
                            <tr>
                                <td><bean:message key="form.password"/></td>
                                <td><html:password property="password"/></td>

                               <td><html:errors property="loginpassword"/></td>

                            </tr>
                            <tr>
                                <td></td>
                                <%
                                Object errorLogin = request.getAttribute("errorLogin");
                                if(errorLogin == "1"){
                                    String error1 = "Login Failed, The email and password you entered don't match.";
                               %>
                                <td><font color="red"><%= error1%></font></td>
                                        <%
                                }
                                %>
                                <td><html:submit value="Login" style="width:70px;height:25px;"/></td>
                            </tr>
                        </table>
                    </html:form>
<!--                     <div id="divNotMember">Not a Member.??</div> -->
<!--                     <div id="divHighlight"><a href="register.jsp">Register</a></div> -->
                </div>
            </div>

    </body>

目录结构 enter image description here

enter image description here enter image description here

enter image description here

enter image description here

当我输入文本框时,我会这样 enter image description here

1 个答案:

答案 0 :(得分:0)

 <servlet>
    <servlet-name>SearchController</servlet-name>
    <servlet-class>com.controller.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SearchController</servlet-name>
    <url-pattern>/Sales/SearchController</url-pattern>
  </servlet-mapping>

刚刚放置了jsp页面所在的所需文件夹,并且效果非常好