FIFA oneterahertz FIFA oneterahertz - 7 months ago 12
Java Question

how a servlet present in a package can be mapped outside welcome file-list

I am getting confused about mapping a servlet.Actually i have two files index.jsp in the webcontent and another file Ihome.jsp in "Sales/Ihome.jsp" of the webcontent.When i run the server and chose "Sales/Ihome.jsp" as the welcome-file and type "s" in a textbox then i am able to display the data as shown below.enter image description here

But when i am chosing "index.jsp" as welcome-file and then clicking on sales and clicking on "Ihome.jsp" and i type a letter in the textbox it is not showing the data as it has shown above.So how to map the servlet so that i can get the data without putting "Sales/Ihome.jsp" in welcome-file list.I am posting the code below

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>



The directory structure
enter image description here

enter image description here
enter image description here

enter image description here

enter image description here

I am getting like this when i type in the textbox
enter image description here

Answer
 <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>

Just placed the required folder in which the jsp page is there and it worked very well