I'm having some problems in creating sessions in JSP pages. I'm new to it so I need some advices.

<%@ page import="entity.*" %>

<% 	
HttpSession session=request.getSession(true);
String id = request.getParameter("idNo");
String fullName = request.getParameter("fullName");
String maritalStatus = request.getParameter("maritalStatus");
String height = request.getParameter("height");
String weight = request.getParameter("weight");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String dateOfBirth = request.getParameter("dateOfBirth");
String placeOfBirth = request.getParameter("placeOfBirth");
String idNo = request.getParameter("idNo");
String countryOfIssue = request.getParameter("countryOfIssue");
String nationality = request.getParameter("nationality");
String race = request.getParameter("race");
String homeNo = request.getParameter("homeNo");
String officeNo = request.getParameter("officeNo");
String mobileNo = request.getParameter("mobileNo");
String address = request.getParameter("address");
String postalCode = request.getParameter("postalCode");
String corrAddress = request.getParameter("corrAddress");
String corrPostalCode = request.getParameter("corrPostalCode");
String emailAddress = request.getParameter("emailAddress");
id = "P-" + id;
session.setAttribute("id", id);
%>

<%

String command1 = request.getParameter("command");

if ((command1 != null) && (command1.equals("insert")))
{
MainApplicant lifeinsured = new MainApplicant();

lifeinsured.setFullName(fullName);
lifeinsured.setMaritalStatus(maritalStatus);
lifeinsured.setHeight(height);
lifeinsured.setWeight(weight);
lifeinsured.setGender(gender);
lifeinsured.setAge(age);
lifeinsured.setDateOfBirth(dateOfBirth);
lifeinsured.setPlaceOfBirth(placeOfBirth);
lifeinsured.setIdNo(idNo);
lifeinsured.setCountryOfIssue(countryOfIssue);
lifeinsured.setNationality(nationality);
lifeinsured.setRace(race);
lifeinsured.setHomeNo(homeNo);
lifeinsured.setOfficeNo(officeNo);
lifeinsured.setMobileNo(mobileNo);
lifeinsured.setAddress(address);
lifeinsured.setPostalCode(postalCode);
lifeinsured.setCorrAddress(corrAddress);
lifeinsured.setCorrPostalCode(corrPostalCode);
lifeinsured.setEmailAddress(emailAddress);

lifeinsured.createMainApplicant();

response.sendRedirect("Step2App.jsp?command=new");
}

%>

I want to insert my ID into the database using session.setAttribute so that when I go to my next JSP page, I can retrieve the ID and use String id = (String)session.getAttribute("id"); to insert the same ID into another table.

But when I run my codes, everything works fine but the ID inserted will become null.

Can anyone help me? Thanks

I'm not sure what you trying to do in that code. Generally JSP will be used for presentation to collect or display data and servlet will take care of validation and communication with database. As for the ID you will either leave it for database to provide you with unique ID or you can come up with simple function that will read last ID from relevant table strip any prefix or postfix to get only number, increment number, and attach any of pre-/post-fix

This article has been dead for over six months. Start a new discussion instead.