Servlet is not Updating DB with new data - java

Im having some little troubles with the UPDATE servlet.
Im trying to update my db but its just not happening. I'm new to this chapter of Java EE.
**NB: I'm just having trouble with the UpdateServlet because i dont know how to get the modified datas from the JSP in order to send it to the DAO and then to update the DB. The rest is OK
The purpose : When the user hits the "Update" button (screenshot below)...
... the JSP forwards the request to the "update user" page (below) where he'll be able to modify the first and last name attached to the email (which is the primaary key)(screenshot below)...
My question is : how do i implement the UpdateUserServlet (see code below) code that gets the User object from the session and updates the database with the new first and last name.
The JSP that displays the User List
<body>
<h1>Users List</h1>
<table cellpadding="5" border=1>
<tr valign="bottom">
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
</tr>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach var="user" items="${users}">
<tr valign="top">
<td><p>${user.firstName}</td>
<td><p>${user.lastName}</td>
<td><p>${user.emailAddress}</td>
<td>Update</td>
<td>Delete</td>
</tr>
</c:forEach>
</table>
</body>
After hitting the "Update button" this JSP below takes over.
....
<body>
<h1>Update User</h1>
<form action="updateUser" method="post">
<table cellspacing="5" border="0">
<tr>
<td align="right">First name:</td>
<td><input type="text" name="firstName"
value="${user.firstName}">
</td>
</tr>
<tr>
<td align="right">Last name:</td>
<td><input type="text" name="lastName"
value="${user.lastName}">
</td>
</tr>
<tr>
<td align="right">Email address:</td>
<td>${user.emailAddress}</td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Submit"></td>
</tr>
</table>
</form>
</body> ....
The Update servlet. Ineed help with this one.
package user;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.User;
import data.UserDB;
public class UpdateUserServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
User user = new User();
HttpSession session = request.getSession();
session.setAttribute("user", user);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmailAddress(emailAddress);
UserDB.update(user);
// TODO: add code that gets the User object from the session and updates the database
String url = "/displayUsers";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
The DAO
package data;
import java.sql.*;
import java.util.ArrayList;
import business.User;
public class UserDB
{
public static int update(User user) {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
String query = "UPDATE User SET " + "FirstName = ?, " + "LastName = ? "
+ "WHERE EmailAddress = ?";
try {
ps = connection.prepareStatement(query);
ps.setString(1, user.getFirstName());
ps.setString(2, user.getLastName());
ps.setString(3, user.getEmailAddress());
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return 0;
} finally {
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
}

Try adding logs to update(User user) method. See whether control is coming to this place if atall.

I found the root cause in the second JSP see the code below.
<tr>
<td align="right">Email address:</td>
<td>${user.emailAddress}</td>
</tr>
The servlet's getParameter("emailAddress") method was actually getting a null value since there is no parameter name in the code above..
So, it should have been done like this:
<tr>
<td align="right">Email address:</td>
<td><input type="text" name="emailaddress"
value="${user.emailaddress}">
</td>
</tr>
Note that the -input type="text"- is not necessary since the email address doesnt have to be modified like the name and firsname. So i should find a way to show the email address in something else than a input text box. But it works now

Related

HTTP Status 405 - Method Not Allowed - while using servlet

My code is to use java servlets and JDBC to store and retrieve the information from a database. There is no error in the IDE the program is running but, the rows aren't inserted into the database and an error occurred in the firefox browser.
The following code is from the SERVLET file
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class ServletRegister extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter("text/html");
String uname = req.getParameter("uname");
String passwd = req.getParameter("passwd");
String email = req.getParameter("email");
int phno = Integer.parseInt(req.getParameter("phno"));
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hack","root","");
PreparedStatement ps = con.prepareStatement("insert into student values(?,?,?,?)");
ps.setString(1,uname);
ps.setString(2,passwd);
ps.setString(3, email);
ps.setInt(4, phno);
int i = ps.executeUpdate();
if(i>0)
out.print("Registerd Successfully");
out.close();
}catch(Exception e) {
System.out.println(e);
}
}
}
The following code is from HTML file
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form action="register">
<table>
<tr>
<td>Enter User Name: <input type="text" name="uname"> </td>
</tr>
<tr>
<td>Enter Password: <input type="password" name="passwd"> </td>
</tr>
<tr>
<td>Enter E-mail: <input type="email" name="email"> </td>
</tr>
<tr>
<td>Enter Phone.no: <input type="number" name="phno" min="6000000000" max="9999999999"></td>
</tr>
<tr>
<td><input type="submit"> <input type="reset"></td>
</tr>
</table>
</form>
</body>
</html>
The following code is from web.xml file
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.hacker.ServletRegister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
The Error is:
Error obtained in firefox browser
You can use Postman to send a POST request to your server, or change doPost to doGet
In HTML file tag you did not mention the method= "post" So its taking the value by default "get" request.

Display retrieved data (from RESTful service) to a table (client)

I have 2 project folders. The first one is a RESTful service and the other one is a client. What I'm trying to do is:
Getting all notes from the RESTful service (I do this as a list) of a specific user.
Then display the notes to the client in a table (html).
When I try to display the notes I get the following error:
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
The HTML code (I'm using JSP). The error appears at the 'forEach' loop:
<table class="table table-striped">
<thead>
<tr>
<!-- Here we create the columns -->
<th> Id </th>
<th> Title </th>
<th> Text </th>
<th> Color </th>
<th> Date/Time </th>
<th> Actions </th> <!-- the table header for Actions -->
</tr>
</thead>
<!-- Table body - The data in the table -->
<tbody>
<c:forEach items="${note-all}" var="pp">
<tr>
<td><c:out value="${pp.id}" /></td>
<td><c:out value="${pp.title}" /></td>
<td><c:out value="${pp.text}" /></td>
<td><c:out value="${pp.color}" /></td>
<td><c:out value="${pp.datetime}" /></td>
<!-- The final column is the Actions, which is a list of buttons,
that we can perfom on our note Entities. -->
<td>
<div class="btn-group">
<!-- ***** Edit Button ***** -->
<a href="#Url.Action("Edit", new {pp.id})" class="btn btn-xs btn-primary">
<i class="glyphicon glyphicon-edit"></i>
Edit
</a>
<a href="#Url.Action("Delete", new {pp.id})" class="btn btn-xs btn-danger" data-post="Are you sure you want to delete this?">
<i class="glyphicon glyphicon-remove"></i>
Delete
</a>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
The RESTful service code:
#Path("/getAll")
#POST
#Consumes({MediaType.APPLICATION_FORM_URLENCODED/})
#Produces({MediaType.APPLICATION_XML})
public Response login(#FormParam("username") String uname
) throws ClassNotFoundException, SQLException{
System.out.println(uname);
//*First*: We get the id of the user
String sql = "SELECT user_id "
+ " FROM user_table "
+ " WHERE username = ?";
PreparedStatement ps = DbCon.getPreparedStatement(sql);
ps.setString(1, uname);
ResultSet rs = ps.executeQuery();
String id = "";
if(rs.next()){
id = rs.getString("user_id");
}
//*Second*: We get the users notes
String sql2 = "SELECT * "
+ " FROM notes_table "
+ " WHERE user_id_fk = ?";
PreparedStatement ps2 = DbCon.getPreparedStatement(sql2);
ps2.setString(1, id);
ResultSet rs2 = ps2.executeQuery();
ArrayList<Note> note_AL = new ArrayList<Note>();
if(rs2.next()){
Note note = new Note();
note.setId(rs2.getInt("note_id"));
note.setTitle(rs2.getString("title"));
note.setText(rs2.getString("text"));
note.setColor(rs2.getString("color"));
note.setDate(rs2.getString("datetime"));
note_AL.add(note);
}
//we wrap the ArrayList with Generic ENtity
GenericEntity<ArrayList<Note>> generic_list_of_notes = new GenericEntity<ArrayList<Note>>(note_AL){};
return Response.ok(generic_list_of_notes).build();
}
The client servlet code (the post method):
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Form form = new Form();
form.add("username", "ali");
//We create a client object
Client client = Client.create();
//We create a resource object and pass a url to it
WebResource webR = client.resource("http://localhost:8080/MyNote/api/notes/getAll");
ClientResponse resp = webR.accept(MediaType.APPLICATION_XML/*"text/html"*/).post(ClientResponse.class, form);
//for debug
if (resp.getStatus() != 200){
System.err.println("Unable to connect to the RESTFUL web service");
}
List<Note> output = resp.getEntity(new GenericType<List<Note>>(){});
request.setAttribute("note-all", output);
RequestDispatcher rd = request.getRequestDispatcher("/Notes.jsp");
rd.forward(request, response);
}

login page using session in jsp

I've created a login environment using jsp. index.jsp, login.jsp. I take username and password from database. If user name and password matches with the database it does login process perfectly. When user give wrong name or password it shows a error message invalid name or passwordand redirect to the login page. Nothing's wrong, but I am facing a problem when I login first. The place where the error message is shown after submitting wrong name or password that place is showing null.
Why null is showing?
Below is my code
index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Example</title>
</head>
<body>
<form action="login.jsp" method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2" align ="left">Login Here</th> <h5><%=request.getAttribute("errorMessage")%> </h5>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
login.jsp
<%# page import ="java.sql.*" %>
<%
String userid = request.getParameter("uname");
String pwd = request.getParameter("pass");
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "root");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from member where uname='" + userid + "' and pass='" + pwd + "'");
if (rs.next()) {
session.setAttribute("userid", userid);
response.sendRedirect("success.jsp");
} else {
request.setAttribute("errorMessage", "Invalid user or password");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
%>
You can use this in your index.jsp
<% if ((String) request.getAttribute("errorMessage") != null) {%>
<h3 style="color: red;"><%=(String) request.getAttribute("errorMessage")%></h3>
<%
}
%>
Instead of this
<%=request.getAttribute("errorMessage")%>
please check condition before showing error, if request parameter is null then it should not be rendered else it would be rendered, modify you code according to below snippet
<thead>
<tr>
<th colspan="2" align ="left">Login Here</th>
<h5>
<%if(request.getParameter("errorMessage")!=null){%>
<%=request.getParameter("errorMessage")%><%}%>
</h5>
</tr>
</thead>
The errorMessage attribute will not be available in request for the first time. Kindly check for a null and then display the message.
<%=request.getAttribute("errorMessage")%>
The below piece of code could help you.
<tr>
<th colspan="2" align ="left">Login Here</th>
<h5>
<%
String errorMsg = request.getAttribute("errorMessage");
if (errorMsg != null) {
%>
<%=errorMsg%>
<%
}
%>
</h5>
</tr>
As errorMessage attribute is null in the request when you load it first time, so you are getting null. You need to add null check for it. You can do this way
<th colspan="2" align ="left">Login Here</th>
<% if (request.getAttribute("errorMessage") != null) {
out.println("<h5>" + request.getAttribute("errorMessage") + "</h5>");
}%>
Correct your code in login.jsp
write session.setAttribute("errorMessage", "Invalid user or password");
instead of request.setAttribute("errorMessage", "Invalid user or password");
..
null message show because of this.

Showing error message on JSP using BindingResult

I am new to Spring.
I am trying to show error messages on jsp for the wrong user and password by using BindingResult. But the error messages are not showing.
Please suggest me what I is wrong in the below code.
JSP
<script type="text/javascript">
function loginUser() {
$('#loginForm').submit();
}
</script>
</head>
<body>
<form:form action="login.test" id="loginForm" commandName="loginForm" method="POST">
<div class="brand_area"></div>
<div class="content_area">
<table style="top: 360px; position: relative; margin-left: 333px;">
<tr id="uNameID">
<td class="label">User Name:</td>
<td><form:input id="userNameID" path="userName" class="textInput" /></td>
<td><form:errors path="userName" class="error"/></td>
</tr>
<tr id="pID">
<td class="label">Password:</td>
<td><form:password id="passwordID" path="password" class="textInput" /></td>
<td><form:errors path="password" class="error"/></td>
</tr>
<tr>
<td></td>
<td><span id="saveButton" class="loginButton"
onclick="loginUser()"> <span>Login</span>
</span></td>
</tr>
</table>
</div>
</form:form>
Controller
#RequestMapping(value = "login.test", method = RequestMethod.POST)
public String processForm( LoginForm loginForm, BindingResult result, ModelMap model, HttpSession session) throws SQLException {
String resultedPage;
model.addAttribute("l_nodes", reportService.getAllLiveNodes());
model.addAttribute("s_nodes", reportService.getAllStaticReportNodes());
User user = userService.getUserByName( loginForm.getUserName() );
if( user != null ){
session.setAttribute("userID", user.getUserID());
if( loginForm.getPassword().equals( user.getPassword() ) ){
resultedPage = "home/userHome";
}else{
result.rejectValue( "password", "login.passwordNotValid");
resultedPage = "redirect:login.test";
}
}else{
result.rejectValue( "userName", "login.userNotValid");
resultedPage = "redirect:login.test";
}
return resultedPage;
}
Thanks
In case anyone else research the same...
Add the hasBindErrors tag to your JSP:
<spring:hasBindErrors name="loginForm">
<c:forEach var="error" items="${errors.allErrors}">
<b><spring:message message="${error}" /></b>
<br/>
</c:forEach>
</spring:hasBindErrors>
Well, i generally send back the values using the Model object.
May be this answer might help you.

Spring SimpleFormController - Including Search Form In Success View

UPDATE 1/31/10: Since this thread continues to get a lot of views...I am curious if it has been of help to anyone recently? Feel free to leave comments/feedback, thanks.
I have a Spring form where I would like to reuse the search page to include the results under the search form. Currently when I do this I get the following error on loading the success view:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'searchAccounts' available as request attribute
Here is my bean configuration:
<bean name="/search.html" class="myapp.web.AccountSearchController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="searchAccounts"/>
<property name="commandClass" value="myapp.service.AccountSearch"/>
<property name="validator">
<bean class="myapp.service.AccountSearchValidator"/>
</property>
<property name="formView" value="accountSearch"/>
<property name="successView" value="accountSearchResults"/>
</bean>
Here is the snippet of JSP that includes the search form:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form method="post" commandName="searchAccounts">
<table valign="top" cellspacing="0" cellpadding="0" width="500" border="0">
<tr>
<td valign="top">
<div class="border-title">Account Search</div>
<div id="navhome">
<div class="border">
<div id="sidebarhome">
<table id="form">
<tr>
<td colspan="2">Search by Account ID or Domain Name. If
values are provided for both, only accounts matching both values
will be returned.</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td align="right" valign="top"><form:label path="accountId">Account ID</form:label>:</td>
<td><form:input path="accountId" size="30"/></td>
</tr>
<c:set var="accountIdErrors"><form:errors path="accountId"/></c:set>
<c:if test="${not empty accountIdErrors}">
<tr>
<td> </td>
<td>${accountIdErrors}</td>
</tr>
</c:if>
<tr>
<td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td>
<td><form:input path="domainName" size="30"/></td>
</tr>
<c:set var="domainNameErrors"><form:errors path="domainName"/></c:set>
<c:if test="${not empty domainNameErrors}">
<tr>
<td> </td>
<td>${domainNameErrors}</td>
</tr>
</c:if>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Search">
</td>
</tr>
</table>
</div>
</div>
</div>
</td>
</tr>
</table>
</form:form>
And...here is my form controller class (less the imports):
public class AccountSearchController extends SimpleFormController {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView onSubmit(Object command, BindException errors) throws ServletException {
String accountId = ((AccountSearch) command).getAccountId();
String domainName = ((AccountSearch) command).getDomainName();
logger.info("User provided search criteria...\n\tDomain Name: " + domainName + "\n\tAccountId: " + accountId);
//TODO do search
logger.info("returning from AccountSearch form view to " + getSuccessView());
return new ModelAndView(getSuccessView());
}
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
AccountSearch accountSearch = new AccountSearch();
return accountSearch;
}
}
Thanks in advance for your help!
-aj
UPDATE:
I ported this to an annotated controller per answer below. Here is the new/working code:
#Controller
#RequestMapping("/search.html")
public class AccountSearchController {
// note: this method does not have to be called setupForm
#RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model) {
AccountSearchCriteria accountSearchCriteria = new AccountSearchCriteria();
model.addAttribute("accountSearchCriteria", accountSearchCriteria);
model.addAttribute("title", "Account Search");
return "accountSearch";
}
// note: this method does not have to be called onSubmit
#RequestMapping(method = RequestMethod.POST)
public String onSubmit(#ModelAttribute("accountSearchCriteria") AccountSearchCriteria accountSearchCriteria, BindingResult result, SessionStatus status, Model model) {
new AccountSearchValidator().validate(accountSearchCriteria, result);
if (result.hasErrors()) {
return "accountSearch";
} else {
ArrayList<AccountSearchCriteria> accountSearchResults = new ArrayList<AccountSearchCriteria>();
AccountSearchCriteria rec = new AccountSearchCriteria();
rec.setDomainName("ajcoon.com");
accountSearchResults.add(rec);
AccountSearchCriteria rec2 = new AccountSearchCriteria();
rec2.setDomainName("ajcoon2.com");
accountSearchResults.add(rec2);
//TODO do search
//ArrayList<HashMap<String,String>> accountSearchResults = new AccountSearchService().search(accountId,domainName);
if( accountSearchResults.size() < 1 ){
result.rejectValue("domainName", "error.accountSearch.noMatchesFound", "No matching records were found.");
return "accountSearch";
} else if(accountSearchResults.size() > 1){
model.addAttribute("accountSearchResults", accountSearchResults);
return "accountSearch";
} else {
status.setComplete();
return "redirect:viewAccount?accountId=";
//return "redirect:viewAccount?accountId=" + accountSearchResults.get(0).getAccountId();
}
}
}
}
try to use (throws Exception instead of ..)
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
AccountSearch accountSearch = new AccountSearch();
System.out.println("inside formBackingObject");
return accountSearch;
}
It looks like your formBackingObject Method is not executed. rerun the code with the above change and see log console to see if the method is executed.
--
You should be using annotation instead of extending controller. Spring 3.0 will deprecate the controller hierarchy.

Categories