Executing MySQL Commands from a Java Servlet - java

I'm trying to get my servlet to output the results of SQL commands entered by the user. Right now, the servlet correctly detects when a command does not include "SELECT", "INSERT", or "DELETE", and outputs an error message. When the command is valid, nothing is outputted. I know this means my problem is likely occurring either where I am trying to connect to the database, or where I am trying to print output to "out".
databaseServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
#SuppressWarnings("serial")
public class databaseServlet extends HttpServlet {
private Connection conn;
private Statement statement;
public void init(ServletConfig config) throws ServletException {
try {
Class.forName(config.getInitParameter("databaseDriver"));
conn = DriverManager.getConnection(
config.getInitParameter("databaseName"),
config.getInitParameter("username"),
config.getInitParameter("password"));
statement = conn.createStatement();
}
catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<xml version = \"1.0\"?>");
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
"XHTML 1.0 Strict//EN\" \"http://www.w3.org" +
"/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<title>MySQL Servlet</title>");
out.println("<style type='text/css'>");
out.println("body{background-color: blue}");
out.println("</style>");
out.println("</head>");
out.println("<body>");
String query = request.getParameter("query");
if (query.toLowerCase().contains("select")) {
//SELECT Queries
try {
ResultSet resultSet = statement.executeQuery(query);
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
for(int i = 1; i<= numberOfColumns; i++){
out.printf("%20s\t", metaData.getColumnName(i));
}
out.println();
while (resultSet.next()){
for (int i = 1; i <= numberOfColumns; i++){
out.printf("%20s\t", resultSet.getObject(i));
}
out.println();
}
}
catch (Exception f) {
f.printStackTrace();
}
}
else if (query.toLowerCase().contains("delete") || query.toLowerCase().contains("insert")) {
//DELETE and INSERT commands
try {
conn.prepareStatement(query).executeUpdate(query);
out.println("\t\t Database has been updated!");
}
catch (Exception l){
l.printStackTrace();
}
}
else {
//Not a valid response
out.println("\t\t Not a valid command or query!");
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
dbServlet.jsp
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- dbServlet.html -->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>MySQL Servlet</title>
<style type="text/css">
body{background-color: green;}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>This is the MySQL Servlet</h2>
<form action = "/database/database" method = "get">
<p>
<label>Enter your query and click the button to invoke a MySQL Servlet
<input type = "text" name = "query" />
<input type = "submit" value = "Run MySQL Servlet" />
</label>
</p>
</form>
</body>
</html>
I thought another potential failure point could be my path to the database file, which is initialized in my web.xml file. An example I found online included the port, but I'm wondering if I should remove the port number. Does anyone know the default port to use for MySQL?
This is the specific line I'm talking about from the xml file below:
jdbc:mysql://localhost:3309/project4
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<!-- General description of the web application -->
<display-name>
MySQL Servlet
</display-name>
<description>
This web application allows the user to connect to a database, sumbit queries, and make changes.
</description>
<!-- Servlet definitions -->
<servlet>
<servlet-name>database</servlet-name>
<description>
A servlet that handles SQL commands submitted by the user.
</description>
<servlet-class>
databaseServlet
</servlet-class>
<init-param>
<param-name>databaseDriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>databaseName</param-name>
<param-value>jdbc:mysql://localhost:3309/project4</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>pass</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>database</servlet-name>
<url-pattern>/database</url-pattern>
</servlet-mapping>
</web-app>

I was using port 3309, and needed to use port 3306.
PROBLEM
jdbc:mysql://localhost:3309/project4
SOLUTION
jdbc:mysql://localhost:3306/project4

Related

Getting more information about an object when clicking on a text in html using MVC

So I'm a making a small web app to get information about games, I have made a searchBar that request the search to an API and then I get the first 10 games with the most similar title to the search. But I only show the title.
This is an example of a search
The games have a lot of attributes, one of them is the ID, and it allows me to get all the info of a game using a controller.
Right now the app works like this:
1: You search the title of a game
2. A controller gives me a List of IDs and with the ids I get the rest of the info about the games, but I only show the titles.
Now, what I want to do:
3.When you click on a title you use a controller, and from the ID it request (again, I'm not sure if I can use the previous games objects) all the info from that game and Shows it.
The controllers works fine, but I'm having problems with the HTML.
This is the controller for the search:
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getParameter("searchQuery");
RequestDispatcher rd = null;
log.log(Level.FINE, "Searching for games that contain " + query);
GameResource game = new GameResource();
GameSearch[] gameResults = null;
gameResults = game.getGameSearch(query);
//System.out.println(Arrays.toString(gameResults));
List<Game> listaBusqueda = new ArrayList<Game>();
if (gameResults.length!=0){
rd = request.getRequestDispatcher("/success.jsp");
for (GameSearch g : gameResults) {
listaBusqueda.add(game.getGame(g.getId().toString()));
}
request.setAttribute("games", listaBusqueda);
} else {
log.log(Level.SEVERE, "Game object: " + gameResults);
rd = request.getRequestDispatcher("/error.jsp");
}
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
...
This is the controllers that gives me a game using an ID
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String gameId = request.getParameter("id");
GameResource resource = new GameResource();
RequestDispatcher rd = null;
log.log(Level.FINE, "Retrieving game");
Game game = resource.getGame(gameId);
if(game!=null) {
rd = request.getRequestDispatcher("/success.jsp");
request.setAttribute("Game", game);
request.setAttribute("Item", "gameID");
} else {
log.log(Level.SEVERE, "Cannot retrieve game: ");
rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Both of them works, in the last one the objects are created correctly and the ID is obtained.
Now, this is the html for success.jsp, and this is where the problem is:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Search results</title>
</head>
<body>
<fieldset id="IGDB">
<legend>
IGDB search for
<c:out value="${param.searchQuery}" />
</legend>
<c:forEach items="${requestScope.games}" var="game">
<article name="gameArticle" id="gameArticle"></article>
<a href="GameIdController?id=${game.id}"><c:out value="${game.name}"
/></a>
<br />
</c:forEach>
</fieldset>
<c:if test="${requestScope.Item=='gameID' }">
<c:forEach items="${requestScope.Game}" var="game">
<article name="gameArticle" id="gameArticle"></article>
<c:out value="${game.name}" />
<br />
</c:forEach>
$game = ${requestScope.Game}
<c:out value="${game.name}"/>
</c:if>
<article name = "gameByIDArticle" id = "gameByIDArticle">
</article>
</body>
</html>
When I click in a title, I get redirected to a blank webpage (http://localhost:8090/GameIdController?id=IdOfTheGame),as you can see the id is included in the URL, but it's completely blank and it should include the title again as is stated in my code.
I will also include my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<description></description>
<display-name>GameSearchController</display-name>
<servlet-name>GameSearchController</servlet-name>
<servlet-class>aiss.controller.GameSearchController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GameSearchController</servlet-name>
<url-pattern>/GameSearchController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>GamePopularityController</display-name>
<servlet-name>GamePopularityController</servlet-name>
<servlet-class>aiss.controller.GamePopularityController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GamePopularityController</servlet-name>
<url-pattern>/GamePopularityController</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/api</param-value>
</context-param>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-
class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-
class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>aiss.api.GameApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>GameIdController</display-name>
<servlet-name>GameIdController</servlet-name>
<servlet-class>aiss.controller.GameIdController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GameIdController</servlet-name>
<url-pattern>/GameIdController</url-pattern>
</servlet-mapping>
</web-app>
Okay i think i understand why you are seeing a blank page. Everything is working as expected, the problem seems to be with this:
if(game!=null) {
rd = request.getRequestDispatcher("/success.jsp");
request.setAttribute("Game", game);
request.setAttribute("Item", "gameID");
} else {
log.log(Level.SEVERE, "Cannot retrieve game: ");
rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
The reason why you are seeing a blank page is because your game variable is not null. And you don't have a forward statement in the first part of that if else block.
if(game!=null) {
rd = request.getRequestDispatcher("/success.jsp");
request.setAttribute("Game", game);
request.setAttribute("Item", "gameID");
rd.forward(request, response); // missing this!
} else {
log.log(Level.SEVERE, "Cannot retrieve game: ");
rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
EDIT: In response to your other problem from the comment, i think the forEach is having problems because of this:
<c:forEach items="${requestScope.Game}" var="game">
change it to this:
<c:forEach items="${Game}" var="game">

Error with Java Servlet & DB connection [duplicate]

This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 years ago.
Having issues getting the program to display anything when running in eclipse.
Goal is:
In this application you will also create a virtual directory so that you do not have to use the word servlet as part of your form post URL. You will then be able to run your Web application servlet by using a URL similar to the following: http://localhost:7070/Week5/FormPost2. To create your virtual directory you may start by modifying the web.xml attached to this assignment. Next, create a Servlet that displays a form when the doGet method is invoked. The form will contain a post action that directs the form post back to the same servlet, which in the doPost method will save the form data to a database. Use your Oracle account to make the DB connection. After the form data has been saved to the database, respond back with a query from the database displaying all the current records contained in the database, in an appealing format. The form must contain a minimum of three input fields.
Below error was received:
HTTP Status 404 - /Week5/servlet/Week5.Week5
type Status report
message /Week5/servlet/Week5.Week5
description The requested resource is not available.
Apache Tomcat/7.0.72
I'm sure it is a simple error, but please pinpoint it so I can get it to display properly. Thank you.
package Week5;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Week5 extends HttpServlet{
private static final long serialVersionUID = 1L;
Connection con = null;
Statement stmt = null;
public Week5(){
init();
}
public void init(){
try{
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE", "student2", "pass");
stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE MYTABLE (FNAME VARCHAR2(20), LNAME VARCHAR2(40), PHONE VARCHAR2(20))");
stmt.close();
}
catch (Exception e){
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("First Name:");
out.println("");
out.println("");
out.println("Last Name:");
out.println("");
out.println("");
out.println("Phone:");
out.println("");
out.println("SUBMIT");
out.println("");
out.println("");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try{
if (con != null)
init();
String fname = request.getParameter("FNAME");
String lname = request.getParameter("LNAME");
String phone = request.getParameter("PHONE");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE VALUES('" + fname + "', '" + lname + "', '" + phone + "')");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
ResultSet rset = stmt.executeQuery("SELECT * FROM MYTABLE");
while (rset.next()){
out.print("");
out.print("First Name: " + rset.getString(1));
out.print("");
out.println();
out.print("");
out.print("Last Name: " + rset.getString(2));
out.print("");
out.println();
out.print("");
out.print("Phone: " + rset.getString(3));
out.print("");
out.println();
out.println();
}
out.println("");
out.close();
stmt.close();
}
catch (Exception e){
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println(e.getMessage());
out.println("");
out.close();
}
}
}
XML
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>
Week5 <!-- Alias I gave the servlet // -->
</servlet-name>
<servlet-class>
HelloWorld <!-- Class name // -->
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
Week5 <!-- Alias I gave the servlet // -->
</servlet-name>
<url-pattern>
/VirtualName <!-- What I want the user to type in // -->
<!-- Example: http://localhost:7070/Week5/FormPost2 // -->
</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>
FormPost2
</servlet-name>
<servlet-class>
HelloWorld2
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
FormPost2
</servlet-name>
<url-pattern>
/VirtualName2
</url-pattern>
</servlet-mapping>
</web-app>
your servlet class should be fully qualified class name such as
<servlet-class>
week5.week5
</servlet-class>
<servlet-class> tag should contain the servlet(class)
<servlet-name> will be the name of the servlet that is mapped with the <servlet-mapping>
example
<servlet>
<servlet-name>
anyServletName
</servlet-name>
<servlet-class>
com.example.customServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
anyServletName
</servlet-name>
<url-pattern>
/VirtualName2
</url-pattern>
</servlet-mapping>

Why does my servlet request.getParameter return null?

I'm having a problem with the request. It always returns "null", but I don't know why. I want it to return a name.
This is my servlet:
public class MinServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Syvtabellen - fra en servlet</title></head>");
out.println("<body>");
out.println("<p>Her er syv-tabellen:<br>");
for (int i=1; i<=10; i++)
{
out.println("Syv gange "+ i +" er: "+ 7*i +".<br>");
}
out.println("</body>");
out.println("</html>");
String parameterværdi = request.getParameter("navn");
out.print( "Værdien af parameteren 'navn' er: <br>" + parameterværdi );
}
}
This is the index.xml:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
This is the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>MinServlet</servlet-name>
<servlet-class>konti.MinServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MinServlet</servlet-name>
<url-pattern>/MinServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Do I've to add it in the index.xml somehow? I know that the parameter returns null if the parameter doesn't exist, but I don't know how to fix it :)
I believe you are following a tutorial. As Elliott said in his comment you need to have a parameter called "navn" in you view to catch it from your servelet otherwise you will get a null. Or else there should be a query string called "navn". Here I can't see any parameter called "navn" in your client side.
For example: http://www.java4s.com/java-servlet-tutorials/example-of-request-getparameter-retrieve-parameters-from-html-form/
According to your code:
index.html
<font face="verdana" size="2px">
<form action="getVal" method="post">
First way to pass request Param <input type="text" name="navn"><br>
<input type="submit" value="Submit">
</form>
</font>
TestApp.java
public class TestApp extends HttpServlet
{
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String n1=req.getParameter("navn");
pw.println("Requested Value" +n1);
pw.close();
}
}
web.xml
<web-app>
<servlet>
<servlet-name>sumOfTwoNumbers</servlet-name>
<servlet-class>java4s.OngetParameter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestApp</servlet-name>
<url-pattern>/getVal</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Or else you can pass query string as following
Second way to pass Request param:
index.html
Click here

Requested Resource not available?

I am new to servlets and jdbc.I just created a Registration page and a HTML registration form. I don't know why I am getting error like : HTTP Status 404 and description for this as The requested page is not available. Here is my servlet, html and .xml files. Please help me with this problem. I am using tomcat 7 and jdk8, in eclipse kepler.
public class Register extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String Name = request.getParameter("Name");
String Email = request.getParameter("Email");
String Password = request.getParameter("Pass");
try {
Class.forName("oracle.jdbc.driver.DriverManager");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:orcl", "scott", "tiger");
PreparedStatement ps = conn
.prepareStatement("Insert into student values(?,?,?)");
ps.setString(1, Name);
ps.setString(2, Email);
ps.setString(3, Password);
int i = ps.executeUpdate();
if (i > 0) {
pw.println("Registered Successfully");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
My Html code.
<body>
<form method="post" action="register">
Name : <input type="text" name="Name"><br/>
Email :<input type="text" name="Email"><br/>
Password :<input type="password" name="Pass"><br/>
<input type="submit" value="register"/>
</form>
and my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
<display-name>SimpleServlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
I am able to hit servlet code using http://localhost:8080/MyWebapp/register.
In my case,Register servlet is in default package.
If your Register servlet is in a package,then in your web.xml,specify class name with package like this.
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>your.package.Register</servlet-class>
</servlet>

Adding data to mysql with servlet not working

This is my database
CREATE TABLE `Animal` ( `name` varchar(128) NOT NULL, `breed` varchar(128) NOT NULL, `age` varchar(128) NOT NULL )
register.html to fill in the data
<html>
<body>
<form action="servlet/Register" method="post">
name <input type="text" name="name"/><br/><br/>
breed <input type="password" name="breed"/><br/><br/>
age <input type="password" name="age"/><br/><br/>
<br/><br/>
<input type="submit" value="register"/>
</form>
</body>
</html>
My servlet
package animals;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Register extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String breed = request.getParameter("breed");
String age = request.getParameter("age");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement ps = con.prepareStatement(
"INSERT INTO Animal (name,breed,age) VALUES(?,?,?)");
ps.setString(1, name);
ps.setString(2, breed);
ps.setString(3, age);
int i = ps.executeUpdate();
if (i > 0) {
out.print("Data successfully registered...");
}
} catch (Exception e2) {
System.out.println(e2);
}
out.close();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>animals.Register</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/servlet/Register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>register.html</welcome-file>
</welcome-file-list>
</web-app>
When I fill in the form and submit, it send me to /register but it's blank and it doesn't add data in my database.
I'm 100% sure that my connection to the java database is working, because I have another project with a login with the same connection to the database and that one is working.
Any tips / comments are welcome
Few things you can check :
1. Debug the code & check if values are captured by the servlet.
2. Use commit() after the executeUpdate(). Maybe your config is set to auto-commit off due to some reasons.
3. Is this string printed? "Data successfully registered..."
4. Lastly, Any exceptions?

Categories

Resources