I am trying to develop a new web application which takes in data from a form and stores data in a database.I have included all the three files join.jsp(the form which takes in data),reg.java(servlet) and JavaConncetDb(method which connects db and servlet).Please do look through and let me know my mistake. I get a 404 error when i try load the web application.Please help me as i beginner in java and trying to learn new things, i am stuck here since a long time
join.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="red">
<form name="register" action="register" method=post>
<p>FIRSTNAME <input type="text" name="firstname"><br>
<p>LASTNAME <input type="text" name="lastname">
<p>EMAILID <input type="text" name="emailid">
<p>USERNAME<input type="text" name="username">
<p>PASSWORD<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
reg.java(servlet)
package logintesting;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.jdbc.OraclePreparedStatement;
/**
* Servlet implementation class reg
*/
@WebServlet("/reg")
public class reg extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public reg() {
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
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String firstname,lastname,email,username,password;
firstname=request.getParameter("firstname").toStri ng();
lastname=request.getParameter("lastname").toString ();
email=request.getParameter("emailid").toString();
username=request.getParameter("username").toString ();
password=request.getParameter("password").toString ();
Connection con=JavaConnectDb.connectDb();
try{
String sql="insert into reg values('"+firstname+"','"+lastname+"','"+email+"', '"+username+"','"+password+"')";
OraclePreparedStatement pst= (OraclePreparedStatement) con.prepareStatement(sql);
pst.execute();
out.println("register successfully");
}
catch(Exception e)
{
out.println(e);
}
finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
javaconnect.java(method used in servlet to conncet to database)
package logintesting;
import java.sql.Connection;
import java.sql.DriverManager;
public class JavaConnectDb {
public static Connection connectDb()
{
Connection conn=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin :@localhost:1521:orcl","SYSTEM","oracle");
}
catch(Exception e)
{
System.err.println(e);
}
return conn;
}
}
No comments:
Post a Comment