1

I'm creating a simple Maven web application in JSP and Java (servlet). The functionality of my webapp is to search through a DB for persons what are registered in it. It searches on ID or name. So I have a JSP with a form that sends the data to my servlet, and in the servlet I call a function from a Java class that makes a DB connection and fetches the data.

But I mentioned that the function never calls or something else, I don't know how. Can anyone please help?

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>TestServlet</servlet-name>
    <display-name>TestServlet</display-name>
    <description></description>
    <servlet-class>mvnproject.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

JSP:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
    <jsp:directive.page contentType="text/html; charset=ISO-8859-1" 
        pageEncoding="ISO-8859-1" session="false"/>
    <jsp:output doctype-root-element="html"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
        omit-xml-declaration="true" />
    <jsp:directive.page import="mvnproject.*" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Serch</title>
</head>
<body>
<h1>Zoek Persoon</h1>
        <form action="TestServlet" method="POST">
            <select name="serchOn">
            <option value="ID" name="ID">ID</option>
            <option value="name" name="name">name</option>
            </select>
            <input type="text" name="input"></input>
            <input type="submit" placeholder="Serch"></input>
        </form>
</body>
</html>
</jsp:root>

Servlet:

package mvnproject.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());

        Functions f = new Functions();

        String name = request.getParameter("serchOn");
        String input = request.getParameter("input");
        System.out.println(" " + name + " " + input);
        System.out.println("calling db conn function");
        f.dbConn(name, input);
        System.out.println("Script completed");

    }



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



    }

}

Java functions:

import java.sql.*;

public class Functions {
  public void dbConn(String nVal, String inpVal){

    System.out.println("Running function...");

    if(nVal != null || inpVal != null){
        String sqlSerch;
        if(nVal.equals("name")){
            sqlSerch = "ID, aNaam FROM profiles WHERE naam = " + nVal;
        }else{
            sqlSerch = "naam, aNaam FROM profiles WHERE ID = " + nVal;
        }

        //driver / db path
            final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
            final String DB_URL = "jdbc:mysql://localhost/profile";
            //DB user&password
            final String USER = "root";
            final String PASS = "Ciber2015!";
            //declare con & sql var
            Connection conn = null;
            Statement stmt = null;
            //register jdbc driver
            try{
                Class.forName(JDBC_DRIVER);
                //make a connection
                conn = DriverManager.getConnection(DB_URL,USER,PASS);
                //SQL Statement
                stmt = conn.createStatement();
                String sql = "SELECT"+ sqlSerch;
                ResultSet rs = stmt.executeQuery(sql);

                //Declareer variablen met data uit db
                int id = rs.getInt("id");
                String naam = rs.getString("naam");

                System.out.println(id + naam);

            }catch(Exception e){
                System.out.println("Eception");
            }           

            System.out.println(" - " + nVal + " - " + inpVal);              
    }
}

Please tell me if I'm doing something wrong.

Marco13
  • 53,703
  • 9
  • 80
  • 159
Casper
  • 293
  • 2
  • 22
  • complete servlet code including web.xml snippet? – Abhinab Kanrar Sep 24 '15 at 09:17
  • Please review this! Your form action must map to the url-pattern under which your servlet is registered in den web.xml http://stackoverflow.com/questions/23751965/how-to-map-a-servlet-call-from-a-jsp-page-using-form-action – swinkler Sep 24 '15 at 09:20
  • Do I understand it right that you see the `calling db conn function` and `Script completed` messages in the console, but no `Running function...` inbetween? – Jozef Chocholacek Sep 24 '15 at 09:21
  • No the print calling db function and everything after it is not shown in the console – Casper Sep 24 '15 at 09:30
  • @swinkler my servlet is called I'm sure of that – Casper Sep 24 '15 at 09:33
  • You will need to change action to `action="/TestServlet"` of form as it should match the URL-Pattern of the servlet. – Rahul Yadav Sep 24 '15 at 09:34
  • @Rahul if I'm doing that I get a 404, it's not that i cant reach the servlet – Casper Sep 24 '15 at 09:39
  • Could you post the console output - I do not clearly understand, what is printed and what is not. Thx – swinkler Sep 24 '15 at 09:45
  • It seems your servlet isn't executed at all. Can you debug it to see, if the doGet / doPost method is executed? Or add `System.out.println("running doGet");` (resp. doPost) as the first line in each method. – Jozef Chocholacek Sep 24 '15 at 09:48
  • This is what I get if I serch on ID and the serch value is 3: ID 3 That's all – Casper Sep 24 '15 at 09:48
  • Your http method is `POST` and your servlet method is `doGet()`. I think it's wrong, they must be identical. `doPost()` method treats `POST` requests and `doGet()` method treats `GET` requests. – drgPP Sep 24 '15 at 09:53
  • My servlet is 100% sure running, else I would not see wich id or name was passed in. And i tried it in the do post first but someone told me that i had to use doGet. So I don't know which one to use anymore @Jozef – Casper Sep 24 '15 at 09:57
  • Tip: be aware, the condition `nVal == "name"` is not how you compare `String`s, use `String` `equals()` method. The line should become if `(nVal.equals("name")) {//code}` – drgPP Sep 24 '15 at 10:05
  • @drgPP I know, your right – Casper Sep 24 '15 at 10:07
  • Well, I do not see any other option than to debug the servlet in an IDE to see what happens after the `System.out.println(" " + name + " " + input);` line. Btw. in your case it does not matter if you use GET or POST method, just be consistent. – Jozef Chocholacek Sep 24 '15 at 11:00

1 Answers1

1

So for the ones what are intrested, The problem wasn't my code but eclipse. It was running a older version of my project while the new one was saved. Thank you for your time gentle(wo)men

Casper
  • 293
  • 2
  • 22