Two submit buttons in a JSP without multiple forms, same controller - java

I have read the similar topics like this, and that one but it did not help with my problem.
I created a simple JavaEE mvc web app. The jsp page contains a form with two text fields and two buttons. The first text field to enter an Id, the second to enter a name. Depending on which button is clicked a Servlet routes to the appropriate method (Search by ID or Search by Name).
Search by id method works correctly. And I see the following path in the address bar: http://localhost:8080/employees_war_exploded/ControllerServlet?textEmployeeId=1&command=Search_ID&employeeName=
However there is a problem with a Search by name method. It does not show any results. Here is what I see in the address bar: http://localhost:8080/employees_war_exploded/ControllerServlet?textEmployeeId=&employeeName=ann&command=Search_Name
I guess the problem is that in both cases it gets the parameters of both text fields ("textEmployeeId" and "employeeName"). How can I make it process both inputs separately in the same form? Maybe there is some other reason for the problem that I do not see?
<form class="form-style" name="form1">
<label>ID:
<input type="text" name="textEmployeeId" value="${tempEmployee.id}" />
</label>
<input type="submit" name="command" value="Search_ID">
<br>
<br>
<label>Name:
<input type="text" name="employeeName" value="${tempEmployee.name}" />
</label>
<input type="submit" name="command" value="Search_Name">
<br>
<br>
</form>
ControllerServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String theCommand = request.getParameter("command");
if (theCommand == null) {
theCommand = "EMPLOYEE_LIST";
}
// route to the appropriate method
switch (theCommand) {
case "EMPLOYEE_LIST":
listEmployees(request, response);
break;
case "Search_ID":
searchById(request, response);
break;
case "Search_Name":
searchByName(request, response);
break;
default:
listEmployees(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void searchByName(HttpServletRequest request, HttpServletResponse response) throws Exception {
String nameString = request.getParameter("employeeName");
List<Employee> namedEmployees = employeeDbUtil.searchEmployees(nameString);
request.setAttribute("EMPLOYEES", namedEmployees);
RequestDispatcher dispatcher = request.getRequestDispatcher("/search-by-name.jsp");
dispatcher.forward(request, response);
}
private void searchById(HttpServletRequest request, HttpServletResponse response) throws Exception {
String textString = request.getParameter("textEmployeeId");
Employee theEmployee = employeeDbUtil.getEmployeeById(textString);
request.setAttribute("THE_EMPLOYEE", theEmployee);
RequestDispatcher dispatcher = request.getRequestDispatcher("/show-employee.jsp");
dispatcher.forward(request, response);
}
}
Update: I'm getting a NullPointerException for the searchByName method. Could there be a problem with this method from a DAO class that is called from ControllerServlet?
public List<Employee> searchEmployees(String employeeName) throws Exception {
List<Employee> employeeList = new ArrayList();
Connection myConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
employeeName += "%";
preparedStatement = myConnection.prepareStatement("SELECT * FROM employees WHERE LOWER(name) like LOWER(?)");
preparedStatement.setString(1, employeeName);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Employee tempEmployee = resultSetToEmployee(resultSet);
employeeList.add(tempEmployee);
}
return employeeList;
}
finally {
DbExceptions.close(resultSet);
DbExceptions.close(preparedStatement);
DbExceptions.close(myConnection);
}
}

Related

Password validation with JSP and Servlet

I have a user Sign in Html form where I get the user's email and password and check them against a database. So far I have the following code but when I submit the form it does not go to the specified JSP page. What can I do to improve my code and how can I just generate an error message when the user presses submit but still stay on the same page?
Thank you in advance.
//SERVLET doPost Method
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userinp = request.getParameter("userinp"); //hidden type in html sign up form
HttpSession s = request.getSession();
User u = new User();
if(userinp.equals("signup")) {
u.setName(request.getParameter("name"));
u.setLname(request.getParameter("lname"));
u.setEmail(request.getParameter("email"));
u.setPassword(request.getParameter("password"));
s.setAttribute("User", u);
//Save to DB
u = (User)s.getAttribute("User");
s.invalidate();
UserM ud = new UserM(); //class which contains CRUD methods
ud.createTable();
ud.insert(u);
ServletContext ctx = request.getSession().getServletContext();
forwardTo(ctx, request, response, "/Somepage.jsp");
} else if(userinp.equals("login")) {
String pass1 = request.getParameter("pass");
String email = request.getParameter("useremail");
Connection conn = null;
PreparedStatement stm = null;
try {
conn = ConnectionConfiguration.getConnection();
stm = conn.prepareStatement("SELECT password FROM users WHERE email = ?");
stm.setString(4, email);
ResultSet resultSet = stm.executeQuery();
while(resultSet.next()) {
String pass2 = resultSet.getString("password");
if(pass1.equals(pass2)) {
ServletContext ctx = request.getSession().getServletContext();
forwardTo(ctx, request, response, "/Somepage.jsp");
} else {
//code to generate "Wrong Password" message
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(stm != null) {
try {
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//ForwardTo Method
public static void forwardTo(ServletContext ctx, HttpServletRequest req, HttpServletResponse resp, String dest) throws ServletException
{
RequestDispatcher rd = ctx.getRequestDispatcher(dest);
try
{
rd.forward(req, resp);
}
catch(Throwable t)
{
t.printStackTrace();
throw new ServletException(t);
}
}
//HTML FORM
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<form action = "UserServ" method="POST">
<h3>Enter the details below to Sign In</h3><br>
Email: <input type="text" name="useremail" required><br>
Password: <input type="password" name="pass" required><br>
<input type="submit" value="Sign In">
</form>
</body>
</html>
You have an error in your database preparedStatement:
stm.setString(4, email);
What is 4 supposed to be here? The first parameter of setString corresponds to the '?' in your prepared statement.
stm = conn.prepareStatement("SELECT password FROM users WHERE email = ?");
You only have 1 question mark, so it should be:
stm.setString(1, email);
What can I do to improve my code
Seperate the database logic from your servlet. Use the MVC pattern, it will make your life easier.
and how can I just generate an error
You can easily achieve this with JSTL/EL in your JSP. Set an attribute in your servlet and forward that to the jsp page. JSTL will check if the attribute exists and show the appropriate message.
You could also just forward the user to a specific page if the details are wrong, like i have shown in the example below.
A more advanced way would be to implement AJAX, this is basically using javascript to make asynchronous calls to your servlet so that you don't have to refresh the page. You could use this to check see if the details are correct.
message when the user presses submit but still stay on the same page?
You mean if they haven't typed in any details? You could use javascript/jquery to do this. Maybe disable the submit btn/form from submitting when the text fields are empty.
Below is your servlet code, i condensed your database logic. Much easier to manage this way:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userinp = request.getParameter("userinp"); //hidden type in html sign up form
HttpSession s = request.getSession();
User u = new User();
ServletContext ctx = s.getServletContext();
//check for null first, because if userinp is empty, then you will get a NPE
if(userinp != null && userinp.equals("signup")) {
u.setName(request.getParameter("name"));
u.setLname(request.getParameter("lname"));
u.setEmail(request.getParameter("email"));
u.setPassword(request.getParameter("password"));
s.setAttribute("User", u);
//Save to DB
u = (User)s.getAttribute("User");
s.invalidate();
UserM ud = new UserM(); //class which contains CRUD methods
ud.createTable(); //why are you creating a table for each user? (you need to create a table called 'users' and just insert the new user there.
ud.insert(u);
forwardTo(ctx, request, response, "/Somepage.jsp");
} else if( userinp != null && userinp.equals("login")) { //you should separate the register and login logic (easier to maintain in two different servlets
String pass1 = request.getParameter("pass");
String email = request.getParameter("useremail");
//so instead of getting the password from the database, you can check to see if the details exist instead and return a boolean.
if(validate(email,pass1)){
forwardTo(ctx, request, response, "/Welcome.jsp"); //user is logged in
}else{
forwardTo(ctx, request, response, "/Error.jsp"); //user is not logged in, details do not match
}
}
}
validate method:
//this should be in a different class. So it's easier to maintain and can be used elsewhere. It's bad practice to have database logic in your servlet. Because what if you want to use this in a different servlet or another part of your application? (you don't want to copy and pasta it everywhere do you?)
public static boolean validate(String email, String password){
boolean status = false;
PreparedStatement pst = null;
ResultSet rs = null;
//if you put your getConnection method as a try condition, it will automagically close the connection for you.
try(Connection conn= ConnectionConfiguration.getConnection()){
pst = conn.prepareStatement("select * from users where email=? and password=?;");
pst.setString(1, email); //1 here corresponds to the first '?' in preparedStatement
pst.setString(2, password); //2 corresponds to the second '?'
rs = pst.executeQuery();
status = rs.next(); //if there are any results, then status is true.
} catch (SQLException e) {
e.printStackTrace();
}
return status;
}
Let me know if you have problems anywhere or other questions, happy to help.

How to fetch data from servlet using ajax?

First of all, this might seem like a duplicate but I assure you I have tried many questions and still hasn't got a proper answer. So I'm asking this here.
I have an HTML form from which I would like to submit a query to a servlet and show the results in a different division.
My HTML code essentially consists of the following:
<form>
<input name="query" id="query" placeholder="Query">
<button id="searchDoc">Search</button>
</form>
<div id="search-results"></div>
I have the following jQuery in order to handle the ajax call.
$('#searchDoc').click(function() {
var q = $('#query').val();
$.ajax({
url: "QueryServlet",
data: {query:q},
success: function(data) {
alert("data: " + data);
$('#search-results').html(data);
}
});
});
My QueryServlet is:
#WebServlet("/QueryServlet")
public class QueryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public QueryServlet() {
super();
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String query = request.getParameter("query");
QueryUtil qu = new QueryUtil();
String mySqlQuery = qu.buildMySQLSearchQuery(query);
System.out.println(mySqlQuery);
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = new DbConnection().getConnection();
st = con.createStatement();
rs = st.executeQuery(mySqlQuery);
if(rs != null) {
response.setStatus(HttpServletResponse.SC_OK);
while(rs.next()) {
out.println("" + rs.getString("fileName") + "");
}
} else {
// TODO add keywords to database
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Even when I submit a valid query, the div does not get loaded up with the data from the servlet. The data reaches the servlet and gets displayed in the console, but I am unable to retrieve that data from the script.
The <button> tag is equivalent to an <input type="submit"/>. If you in your form tag don't declare any action attribute, the standard action causes that the page is reloaded. This causes that, although the returned data are inserted in #search-results div, you'll never be able to see them, because the page is immediately reloaded.
You should deactivate the default "Submit" button this way:
$('#searchDoc').click(function(e) {
e.preventDefault();
[....]
});
This should fix your problem!
the issue seems related to context path, your path should look like this if servlet is not in context root :-
<host> / <context path>/ <servlet>
Thanks :)

Uncaught TypeError: Illegal invocation

I am trying to delete users from my database using Ajax, Servlet and HTML. when I submit data Illegal Invocation occurs. I think that there won't be any problems with connection or SQL statement
delete.html
<input type="text" id="delete">
<input type="submit" onclick="deleteUSer()" value="Delete">
delete.js
function deleteUSer(){
var username = document.getElementById("delete");
var params = {
username: username
}
$.post("Delete", params, function(data){}
)
}
Delete.java servlet
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = null;
DBUtils dbutils = null;
Connection conn = null;
try{
String username = request.getParameter("username");
dbutils = new DerbyUtils();
conn = dbutils.getConnection();
DeleteDAO dao = new DeleteDAO(conn);
dao.deleteUser(username);
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/index.html");
dispatcher.forward(request, response);
}
DeleteDAO.java
public void deleteUser(String username) throws SQLException{
try{
String sql = "delete from users where username='"+username+"'";
PreparedStatement ps = this.conn.prepareStatement(sql);
ps.executeQuery();
you are sending html doc elemect not username ...
try this --
<input type="text" id="delete" value="vivek">
<input type="submit" onclick="deleteUSer()" value="Delete">
function deleteUSer(){
var username = document.getElementById("delete");
var params = {
username: username.value
}
$.post("Delete", params, function(data){}
)
}
If you're trying to enact the "delete" command that you can use with HTTP requests, you're going to want to use $.ajax, not $.post.
To quote the docs (https://api.jquery.com/jQuery.post/):
This is a shorthand Ajax function, which is equivalent to:
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
if you're trying to enact the "delete", you'd want to do something like:
var dataObj = {
'username': document.getElementById("delete").value;
}
$.ajax({
type:"DELETE",
data: dataObj,
url: 'url/to/your/servlet'
});
You may also want to include a datatype in there but for this example jquery can figure it out.

why data getting insert after dispatched as invalid in google app engine java

Iam trying to validate and insert a data. here if the username exists in file service it should dispatch with error message but after the validation it is dispatched with error message with inserting data in file service. I cant fine where am failing in code.
My JSP:
<form name="create" id="myform" action="/create" method="post">
User Name: <input type="text" name="cliname"/>
<input type ="submit" value="submit"/>
</form>
My Servlet:
public class CreateForm extends HttpServlet {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
String uname = req.getParameter("cliname");
Query query1 = new Query("Users");
List<Entity> cli_id = datastore.prepare(query1).asList(FetchOptions.Builder.withDefaults());
for (Entity client : cli_id){
username = (String)client.getProperty("User Name");
if(username.equals(uname)) {
RequestDispatcher rd = req.getRequestDispatcher("/Create.jsp");
req.setAttribute("errormsg", "User Name Already Exists");
rd.forward(req, resp);}}
Entity userInput = new Entity("Users");
userInput.setProperty("User Name", uname);
datastore.put(userInput);
}}
Kindly suggest me an idea,
Your help is appreciated.
I have updated your code.
public class CreateForm extends HttpServlet {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String uname = req.getParameter("cliname");
Query query1 = new Query("Users");
List<Entity> cli_id = datastore.prepare(query1).asList(FetchOptions.Builder.withDefaults());
RequestDispatcher rd = null;
for (Entity client : cli_id){
username = (String)client.getProperty("User_Name");
if(username.equals(uname)) {
req.setAttribute("errormsg", "User Name Already Exists");
rd = req.getRequestDispatcher("/error.jsp");
}else{
Entity userInput = new Entity("Users");
userInput.setProperty("User_Name", uname);
datastore.put(userInput);
req.setAttribute("success", "User Name Added");
rd = req.getRequestDispatcher("/Create.jsp");
}
}
rd.forward(req, resp);
}
}

Getting information from servlet created html

I have a servlet that creates an html text box and then redirects to another servlet on submit. How can I access the value of the html text box from the new servlet? I am able to access servlet variables from the new servlet but I am not aware of how to access the value of the html generated code.
thanks,
Here is the servlet that gets the text input
public class ServletB extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
String value = System.getProperty("card");
PrintWriter out = response.getWriter();
out.println("<center><h1>Your preffered method of payment is "+value+"</h1><br />");
out.println("Please Enter Card Number<input type =\"text\" name = \"number\"/><form action=\"http://codd.cs.gsu.edu:9999/cpereyra183/servlet/ServletC\"><input type =\"submit\" value=\"Continue\" /><input type=\"button\" value=\"Cancel\" /></center>");
}
}}
This is the servlet the first servlet redirects to all I do is try to do is output the text input
public class ServletC extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
String value = System.getProperty("card");
PrintWriter out = response.getWriter();
out.println(request.getParameter("number"));
}
}
If you give the input field a name
<input type="text" name="foo">
then you can access it in the postprocessing servlet as a request parameter by the input field's name.
String foo = request.getParameter("foo");
See also:
Servlets info page - contains a hello world
Unrelated to the concrete question, in contrary to what the majority of servlet tutorials want to let believe us, HTML actually belongs in JSP, not in a Servlet. I'd suggest to put that HTML in a JSP.
If your markup looks something like this...
<form action="anotherServlet">
<input name="myTextbox" />
</form>
...then you can get the value out of the HttpServletRequest object in the doGet() or doPost() method of anotherServlet like this:
String textboxValue = request.getParameter("myTextbox");
See: ServletRequest#getParameter().
public class Formvalid extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pr = response.getWriter();
boolean flag = true;
int count=0;
response.setContentType("text/html");
Enumeration enume;
enume = request.getParameterNames();
while (enume.hasMoreElements()) {
count++;
String name = (String) enume.nextElement();
String value = request.getParameter(name);
if (value == null || value.equals("")) {
pr.println("<h5 style='color:red;'>please enter manditory values :"
+ name + "</h5>");
flag = false;
}
}
pr.println("<h3>Employe Registation</h3>");
if (!flag || count==0) {
pr.println("<form method=\"get\" action=\"formvalid\"><br />EmpName *:<input type='text' name='Empname' ><br />"
+ "Age *:<input type='text' name='age' ><br /><tr><td>Qulification *:<input type='text' name='Qualification' ><br />Address<textarea> </textarea><br /><input type='submit' value='submit'><input type='reset' value='reset'></FORM>");
} else {
pr.println("<h3 style='color:green;'>submitted successfully</h3>");
}
}
}

Categories