I am developing the project for college, I am new to this interface and need help for this concern. I have created a DB on the mysql using SQLyog and Dynamic web project on eclipse. For the registration I have used the following code:
register.jsp
<form action="register1.jsp" method="post">
<div class="firstname">
<label for="firstname"></label> <input type="text"
placeholder="My first name is" name="firstname" required>
</div>
<div class="lastname">
<label for="lastname"></label> <input type="text"
placeholder="My last name is" name="lastname" required>
</div>
<div class="email">
<label for="email"></label> <input type="email"
placeholder="My e-mail ID is" name="email_id" required>
</div>
<div class="username">
<label for="username"></label> <input type="text"
placeholder="My username is" name="username" required>
</div>
<div class="pwd">
<label for="pwd"></label> <input type="password"
placeholder="My password is" name="pwd" required>
</div>
<div class="submit">
<input type="submit" value="Register" id="form_button" />
</div>
</form>
register1.jsp
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
String username=request.getParameter("username");
session.putValue("username",username);
String pwd=request.getParameter("pwd");
String email_id=request.getParameter("email_id");
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university","CinthiyaSingh", "Cinthiya@098");
Statement st=con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into users values('"+ username+ "','"+pwd+ "','"+firstname+ "','"+ lastname+ "','"+ email_id+ "')");
if (i > 0) {
response.sendRedirect("login.jsp");
} else {
request.setAttribute("errorMessage", "Already Existing Username/Email");
request.getRequestDispatcher("Register.jsp").forward(request, response);
}%>
I want the registration page to reload when the query tries to violate the primary key (username) property and display the error of :"username already in use". However, the error page of the server is loaded with the error details.
Please look into this. I don't want to use any other file preferably.