Skip to content

Commit

Permalink
fix problems of expierd sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
snirye committed Apr 19, 2020
1 parent f280443 commit f0510ec
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 127 deletions.
17 changes: 5 additions & 12 deletions src/controller/AddItemHandlerServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,12 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
response.setContentType("text/html");
PrintWriter out=response.getWriter();


model.Model mod = (model.Model)getServletContext().getAttribute("model");
HttpSession session=request.getSession(false);
if(session==null){
request.setAttribute("page","content/LoginFirst.jsp");
}
else
{
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user==null) {
request.setAttribute("page","content/LoginFirst.jsp");
} else {
if(user!=null){
String ItemName=request.getParameter("name");
String ItemCategory=request.getParameter("category");
String ItemValue=request.getParameter("value");
Expand All @@ -103,7 +97,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)


if(picture.equals("1")) {
System.out.println("we have file");
String fileName = getSubmittedFileName(filePart); // MSIE fix.
InputStream fileContent = filePart.getInputStream();

Expand All @@ -112,7 +105,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
//create new folder:
// C:\\Users\\SnirY\\OneDrive\\Documents\\Bar Ilan\\sems5\\Software eng\\ShareIT\\WebContent\\ItemPictures\\
String upload_path=getServletContext().getInitParameter("upload_location")+ItemID;
System.out.println("upload_path is : " + upload_path);
// System.out.println("upload_path is : " + upload_path);

new File(upload_path).mkdir();
File uploads = new File(upload_path);
Expand All @@ -125,7 +118,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}

//show list of pictures
System.out.println("file path at : " +file.getPath() );
// System.out.println("file path at : " +file.getPath() );
}

StringBuffer msg=new StringBuffer("<h1>You added a new item:</h1>");
Expand Down
34 changes: 17 additions & 17 deletions src/controller/AddItemServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
@WebServlet("/AddItemServlet")
public class AddItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddItemServlet() {
super();
}

/**
* @see HttpServlet#HttpServlet()
*/
public AddItemServlet() {
super();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
Expand All @@ -34,21 +34,21 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
request.setAttribute("title","New Item");

model.Model mod = (model.Model)getServletContext().getAttribute("model");
HttpSession session=request.getSession(false);
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
String name=(String)session.getAttribute("name");
request.setAttribute("name",name);
request.setAttribute("page","content/AddItem.jsp");
}
else{
request.setAttribute("page","content/LoginFirst.jsp");
HttpSession session=request.getSession(false);
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
String name=(String)session.getAttribute("name");
request.setAttribute("name",name);
request.setAttribute("page","content/AddItem.jsp");
}
}
RequestDispatcher rd=request.getRequestDispatcher("template.jsp");
rd.forward(request, response);
out.close();
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
Expand Down
71 changes: 40 additions & 31 deletions src/controller/ChangeRequestResponeServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,63 @@
@WebServlet("/ChangeRequestResponeServlet")
public class ChangeRequestResponeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ChangeRequestResponeServlet() {
super();
}

/**
* @see HttpServlet#HttpServlet()
*/
public ChangeRequestResponeServlet() {
super();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();

request.setAttribute("title","Request Approve");

model.Model mod = (model.Model)getServletContext().getAttribute("model");
model.Requests req=mod.getRequestByID(request.getParameter("req_id"));
String newResp=request.getParameter("new_resp");
HttpSession session=request.getSession(false);
if(session==null){
request.setAttribute("page","content/LoginFirst.jsp");
}
else
{
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user==null) {
request.setAttribute("page","content/LoginFirst.jsp");
} else {
if(user!=null){
String name=(String)session.getAttribute("name");
request.setAttribute("name",name);
if(req.getOwnerID().equals(user.getUserID())) {
//TODO:check if owner to aprove and deny and dnoe. check if borower if cancel
//TODO:add user details page
req.setResponse(newResp);
mod.editRequest(req);
String msg;
if(newResp.equals("1")) {msg="request approved";}
if(newResp.equals("2")) {msg="request Denyed";}
if(newResp.equals("3")) {msg="request Done";}
if(newResp.equals("4")) {msg="request Canceled";}
else {msg="Unknown request status";}

request.setAttribute("message", msg);
String msg="";
if(newResp.equals("1")||newResp.equals("2")||newResp.equals("3")) {
if(req.getOwnerID().equals(user.getUserID())) {
//TODO:check if owner to aprove and deny and dnoe. check if borower if cancel
//TODO:add user details page

if(newResp.equals("1")) {msg="request approved";}
else if(newResp.equals("2")) {msg="request Denyed";}
else if(newResp.equals("3")) {msg="request Done";}
req.setResponse(newResp);
mod.editRequest(req);
} else {
msg= "you are not the owner of this item! go away!";
}
} else if(newResp.equals("4")){
if(req.getBorrowerID().equals(user.getUserID())) {
{msg="request Canceled";}
req.setResponse(newResp);
mod.editRequest(req);
} else {
//not the request creaator:
msg="you are not the owner of this request! go away!";

}
} else {
request.setAttribute("message", "you are not the owner! go away!");
//some strange respone code:
msg="What are you trying to do with respone code="+newResp+"?";
}

request.setAttribute("message", msg);
request.setAttribute("page","content/FreeMessage.jsp");
}

Expand Down
4 changes: 0 additions & 4 deletions src/controller/LoginServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
System.out.println("loginServlet");
model.Model mod = (model.Model)getServletContext().getAttribute("model");


Expand All @@ -48,9 +47,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)


model.Users user = mod.login(id, password);
System.out.println("user = "+id);
System.out.println("password = "+password);
System.out.println("userID = "+user.getUserID());
if(user.getUserID() != null) {
// if(password.equals("1234")) {

Expand Down
14 changes: 7 additions & 7 deletions src/controller/MyItemsServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
PrintWriter out=response.getWriter();
request.setAttribute("title","My Items");
model.Model mod = (model.Model)getServletContext().getAttribute("model");
HttpSession session=request.getSession(false);
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
HttpSession session=request.getSession(false);
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){

String name=user.getUserName();
List<model.Items> results=mod.getItemsByUserID(user.getUserID());
request.setAttribute("items", results);
request.setAttribute("name",name);
request.setAttribute("page","content/MyItems.jsp");
}
else{
request.setAttribute("page","content/LoginFirst.jsp");
}
}
}
RequestDispatcher rd=request.getRequestDispatcher("template.jsp");
rd.forward(request, response);
out.close();
Expand Down
26 changes: 13 additions & 13 deletions src/controller/OffersServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
request.setAttribute("title","Offers");

model.Model mod = (model.Model)getServletContext().getAttribute("model");
HttpSession session=request.getSession(false);
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
String name=(String)session.getAttribute("name");
List<model.Requests> offers=mod.getSentRequestsByUserID(user.getUserID());
request.setAttribute("name",name);

request.setAttribute("requests",offers);
request.setAttribute("page","content/Offers.jsp");
}
else{
request.setAttribute("page","content/LoginFirst.jsp");
}
HttpSession session=request.getSession(false);
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
String name=(String)session.getAttribute("name");
List<model.Requests> offers=mod.getSentRequestsByUserID(user.getUserID());
request.setAttribute("name",name);
request.setAttribute("requests",offers);
request.setAttribute("page","content/Offers.jsp");
}
}
RequestDispatcher rd=request.getRequestDispatcher("template.jsp");
rd.forward(request, response);
out.close();
Expand Down
29 changes: 11 additions & 18 deletions src/controller/ProfileServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,33 @@
@WebServlet("/ProfileServlet")
public class ProfileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProfileServlet() {
super();
}
/**
* @see HttpServlet#HttpServlet()
*/
public ProfileServlet() {
super();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();

request.setAttribute("title","Profile");

model.Model mod = (model.Model)getServletContext().getAttribute("model");
HttpSession session=request.getSession(false);
if(session==null){
request.setAttribute("page","content/Login.jsp");
}
else
{
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user==null) {
request.setAttribute("page","content/Login.jsp");
} else {
if(user!=null){
String name=(String)session.getAttribute("name");
request.setAttribute("name",name);
request.setAttribute("good","yes");
request.setAttribute("page","ProfileDet.jsp");

System.out.println("Hello, "+name+" Welcome to Profile");
System.out.println("id is: " + session.getId());

}

}
Expand Down
15 changes: 9 additions & 6 deletions src/controller/ReqestItemServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ public ReqestItemServlet() {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.setAttribute("title","Request Item");


String itemId=request.getParameter("id");
model.Model mod = (model.Model)getServletContext().getAttribute("model");
HttpSession session=request.getSession(false);
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
HttpSession session=request.getSession(false);
request.setAttribute("page","content/LoginFirst.jsp");
if(session!=null) {
model.Users user=(model.Users)session.getAttribute("user");
if(user!=null){
// "itemID", "owner(userID)", "borrower(userID)", "period", "response"
String[] data= {itemId,user.getUserID(),"week",""};
System.out.println("passing :"+data);
Expand All @@ -52,9 +56,8 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
request.setAttribute("message", "item requested seccesfully!");
request.setAttribute("title", "Item Requested");
request.setAttribute("page","content/FreeMessage.jsp");
} else {
request.setAttribute("page","content/LoginFirst.jsp");
}
}
}

RequestDispatcher rd=request.getRequestDispatcher("template.jsp");
rd.forward(request, response);
Expand Down
Loading

0 comments on commit f0510ec

Please sign in to comment.