Servlet Code:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
throw new ServletException();
}
UserOperations uo = new UserOperations(conn);
if (action.equals("mainbutton")) {
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String address = request.getParameter("address");
String country = request.getParameter("country");
String state = request.getParameter("state");
String postalCode = request.getParameter("postalcode");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
User user = new User(name, surname, address, country, state,
postalCode, phone, email);
try {
if (user.validate()) {
uo.create(name, surname, address, country, state,
postalCode, phone, email);
} else {
request.setAttribute("message", user.getMessage());
System.out.println(request.getAttribute("message"));
request.getRequestDispatcher("index.jsp").forward(request,
response);
}
} catch (SQLException e) {
System.out.println("No connection to the database.");
e.printStackTrace();
}
}
try {
conn.close();
} catch (SQLException e) {
throw new ServletException();
}
}
JSP code:
<script type="text/javascript">
$(document).ready(function(){
$("#mainbutton").click(function(){
var name=$('input[name=name]').val();
var surname=$('input[name=surname]').val();
var address=$('input[name=address]').val();
var country=$('#country :selected').val();
var state=$('#state :selected').text();
var postalcode=$('input[name=postalcode]').val();
var phone=$('#mobile-number').val();
var email=$('input[name=email]').val();
$.post("userctrl",
{
action: "mainbutton",
name: name,
surname: surname,
address: address,
country: country,
state: state,
postalcode: postalcode,
phone: phone,
email: email
});
});
});
</script>
<li>
<p class="valid-error">
<%= request.getAttribute("message") %>
</p>
</li>
<li>
<input id="mainbutton" class="mainbutton" type="button" value="Rush"/>
</li>
On click of rush button I fill the request with atribute "message". After redirect to the same page I see only null. How to get my request atribute? The sysout is printed to the console. Also tried to redirect to index.jsp
instead of /index.jsp
without success.