0

I am trying to make a simple application using servlets where I retrieve data from a user like name, surname, age and phone number which I then store in a database after insertion but I am having difficulties in doing it because when it comes to compiling and deploying using ant I get no errors but after clicking the submit button from my form, I get an error. I don't what might be the cause of the problem.I have both my servlet and database jar files in my lib folder before compiling and deployment but after deploye only one jar file is properly deploy.Any shed of light is really appreciated. Thank you in advance. I am thinking maybe my build.xml should be modified so both my jars files can be properly deployed. I get the following error.

I have included my build.xml file.


System for Mentoring 201103

<property name="name" value="servlet"/>
<property name="project.code" value="servlet"/>
<property name="project.name" value="servlet"/>
<property name="version" value="1.00.00"/>

<!--*** Source properties ***-->
<property name="src" location="src"/>
<property name="src-main" location="${src}/main"/>  
<property name="src-main.java" location="${src-main}/java"/>
<property name="src-main.resources" location="${src-main}/resources"/>
<property name="src-main.webapp" location="${src-main}/webapp"/>
<property name="src-test" location="${src}/test"/>      
<property name="src-test.java" location="${src-test}/java"/>
<property name="src-test.resources" location="${src-test}/resources"/>

<property name="lib" location="lib"/>

<!--*** Target properties ***-->
<property name="target" location="target"/>
<property name="build" location="${target}/build"/>
<property name="build.classes" location="${build}/classes"/>
<property name="deploy" location="${target}/deploy"/>
<property name="ide" location="${target}/ide"/>

<!--*** Tomcat Properties ***-->
<property name="tomcat-webapps" location="C:\apache-tomcat-7.0.37-windows-x64\apache-tomcat-7.0.37\webapps"/>

<!--*** Deploy properties ***-->
<property name="tomcat.deploy.dir" location="${tomcat-webapps}"/>

    <!--*** Classpath settings ***-->
<path id="classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar"/>

    </fileset>
    <pathelement path="${build}/classes"/>
</path>

<!--***************************************************************************************************************
  * Target: Help (Default)
  ***************************************************************************************************************-->
<target name="help" description="Gives help on all tasks in this build file">
    <echo>Run "ant -projecthelp" or "ant -p" for a list of valid targets for this ant buildfile."</echo>
</target>

<!--***************************************************************************************************************
  * Target: createTempDirs
  ***************************************************************************************************************-->
<target name="init:create-temp-dirs" description="Creates all relevant temporary directoriees. Typically only used the first time these directories need to be created.">
    <mkdir dir="${target}"/>
    <mkdir dir="${build}"/>
    <mkdir dir="${build.classes}"/>
    <mkdir dir="${deploy}"/>
    <mkdir dir="${ide}"/>
    <mkdir dir="${ide}/classes"/>
</target>

<!--***************************************************************************************************************
  * Target: Clean
  ***************************************************************************************************************-->
<target name="clean" description="Deletes all relevant files and directories eg. build classes, build directories.">
    <!-- Kill the build directory -->
    <delete dir="${build}" />

    <!-- Delete backup files, if necessary -->

    <!-- Delete all temporary files, if necessary -->
</target>

<!--***************************************************************************************************************
  * Target: prepare
  ***************************************************************************************************************-->
<target name="prepare" depends="init:create-temp-dirs" description="Creates all relevant dirs, expand jars, etc.">
    <mkdir dir="${build}"/>
    <mkdir dir="${build.classes}"/>
</target>

<!--***************************************************************************************************************
  * Target: java
  ***************************************************************************************************************-->
<target name="_compile" depends="prepare" >
    <javac srcdir="${src-main.java}" destdir="${build.classes}">
        <classpath refid="classpath"/>
        <include name="**/*.java"/>
    </javac>
    <!-- copy all relevant confirguration files -->
    <copy todir="${build.classes}/resources">
        <fileset dir="${src-main}/resources"/>
    </copy>
    <!-- copy all file in root java directory. -->
    <copy todir="${build.classes}">
        <fileset dir="${src-main.java}" includes="*.*"/>
    </copy>
</target>

<target name="compile" depends="clean, _compile" 
          description="Does a clean Java compile ie. all previous built classes are first deleted and then a full recompile takes place." />
<target name="compile:fast" depends="_compile"
        description="Does a Java compile only on changed files. Previously compiled files are left untouched." />

<!--***************************************************************************************************************
  * Target: war related
  ***************************************************************************************************************-->
<property name="deploy.warfile.name" value="${deploy}/${project.name}.war"/>

<!-- Do a WAR build from the given build directory. No Hibernate build is done. -->
<target name="war:build" description="Creates the War file for this project.">
    <!--<delete file="${deploy.warfile.name}" />-->
    <war destfile="${deploy.warfile.name}" webxml="${src-main.webapp}/WEB-INF/web.xml">
        <classes dir="${build.classes}" />
        <webinf dir="${src-main.webapp}/WEB-INF" />
        <fileset dir="${src-main.webapp}" excludes="WEB-INF/**/*.*" />
        <lib dir="${lib}" includes="**/*.jar" excludes="servlet*.jar, jsp*.jar, javax*.jar" />
    </war>
</target>

<!-- Do a WAR Build and Deploy To Tomcat -->
<target name="war:deploy" depends="war:build" description="Builds the war file (war:build) and deploys it to Tomcat.">
    <copy file="${deploy.warfile.name}" todir="${tomcat.deploy.dir}" />
</target>

enter image description here

2 Answers 2

1

Please share your build.xml. I think your task which is creating the war file is not copying the jar files in war.

<target name="war" depends="build" description="War up the compiled classes into ${package.file.war}">
    <war destfile="${package.file.war}">
        <fileset dir="build" includes="**" excludes="**/*.java"/>       
    </war>
</target>

[EDIT]There is no issue in your build file. Make sure your source code contain this statement

Class.forName("com.mysql.jdbc.Driver");
Sign up to request clarification or add additional context in comments.

3 Comments

@ Sohailehmad thank you for your reply. I have placed my build.xml
i think you are missing Class.forName("com.mysql.jdbc.Driver");
I managed to figure out the problem, when I was creating the war file in the build file, I was excluding certain jar file names , so I realized I was also excluding one of the libraries I needed for compiling hence it was deploying the mysql-connector jar file only and not deploying the servlet library as it had the same name as the ones I was excluding, i removed it from exclusion and i worked. Thanks a lot once again Sohailehmad.
0

You do not have mysql-connector jar file in your WEB-INF/lib folder

make sure you have the following things at place

1) in WEB-INF/lib folder copy paste all the jar dependencies you use

2) in WEB-INF/classes folder you should have your .class files

3) have your index.html or index.jsp file in Root folder of your web application

4) and have your web.xml in WEB-INF folder

you can find the required jar file in MySQL\MySQL Tools for 5.0\java\lib folder

1 Comment

@ Bhavik, thank for your reply. I do have the servlet 2.5 jar and mysql-connectort jar file in my lib folder, the problem is after I deploy, only one library is properly deployed in the war file and the other is not present. So I don't knw if maybe my build.xml might be the cause of that because I have the following under my build.xml file. <!--*** Classpath settings **--> <path id="classpath"> <fileset dir="${lib}"> <include name="**/.jar"/> </fileset> <pathelement path="${build}/classes"/> </path>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.