How to create a class to store an Object List and display in the JSP without deleting the list?
Good night, I'm learning JAVA WEB + JSP + SERVLETS and as I'm new to both I have the following problems:
I am using the MVC architecture to create the models, views and controllers of my application.
My application works as follows:
In the same route I should make CRUD from a shopping list, for this I have two classes in two different packages which are:
Models - Products.java
Services - Shopping.java
In the Products class I use to instantiate a new item with the code, name and quantity of the product, then with the Purchases I use to store a List of these products and be able to use it in my JSP ...
The question is:
1) How do I create a List of these instantiated objects and do the methods of the code below?
2) How do I display this list in my JSP ..
3) How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?
4) How do I import classes from other packages into my servlet?
Compras.java
public class Compras {
private List<Object> listaProdutos = new List<Object>();
public Compras(List<Object> listaProdutos){
this.listaProdutos = listaProdutos;
}
//Adds a new product to the List
private addProduct(){}
//Removes a specific product from the list
private removeSpecificProduct(){}
//Get all products from the list
private getAllProducts(){}
//Get a specific product from the list
private getSpecificProduct(){}
// Take the size of the list
private getListSize(){}
//Change the position of a specific product in the list
private changeSpecificProductPosition(){}
}
Produtos.java
public class Produtos {
private int quantity;
private String name;
public Produtos(int quantity, String name){
this.quantity = quantity;
this.name = name;
}
}
Servlets - Add.java
@WebServlet(name = "Add", urlPatterns = {"/Add"})
public class Add extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// REQUEST PRODUCT
String productName = request.getParameter("productName").toUpperCase();
int productQuantity = Integer.parseInt(request.getParameter("productQuantity"));
// NEW PRODUCT
Product newProduct = new Product(productName, productQuantity);
}
}
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 Page</title>
</head>
<body>
<div>
<h1><strong>Lista de Compras</strong></h1>
<hr>
<div></div>
</div>
<div>
<h1>INCLUSÃO DE UM NOVO ITEM</h1>
<form action="/add" method="POST" style="background-color: dimgray; padding: 10px;">
<div style="padding: 5px">
<label style="color: white;"><strong>Item:</strong></label>
<input name="productName" type="text" required></input>
</div>
<hr>
<br>
<div>
<label style="color: white;"><strong>Quantidade:</strong></label>
<input name="productQuantity" type="number" min="1" required></input
</div>
<hr>
<div style="padding-top: 30px;">
<button type="submit">ADICIONAR</button>
</div>
</form>
</div>
</body>
</html>
java jsp servlets
add a comment |
Good night, I'm learning JAVA WEB + JSP + SERVLETS and as I'm new to both I have the following problems:
I am using the MVC architecture to create the models, views and controllers of my application.
My application works as follows:
In the same route I should make CRUD from a shopping list, for this I have two classes in two different packages which are:
Models - Products.java
Services - Shopping.java
In the Products class I use to instantiate a new item with the code, name and quantity of the product, then with the Purchases I use to store a List of these products and be able to use it in my JSP ...
The question is:
1) How do I create a List of these instantiated objects and do the methods of the code below?
2) How do I display this list in my JSP ..
3) How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?
4) How do I import classes from other packages into my servlet?
Compras.java
public class Compras {
private List<Object> listaProdutos = new List<Object>();
public Compras(List<Object> listaProdutos){
this.listaProdutos = listaProdutos;
}
//Adds a new product to the List
private addProduct(){}
//Removes a specific product from the list
private removeSpecificProduct(){}
//Get all products from the list
private getAllProducts(){}
//Get a specific product from the list
private getSpecificProduct(){}
// Take the size of the list
private getListSize(){}
//Change the position of a specific product in the list
private changeSpecificProductPosition(){}
}
Produtos.java
public class Produtos {
private int quantity;
private String name;
public Produtos(int quantity, String name){
this.quantity = quantity;
this.name = name;
}
}
Servlets - Add.java
@WebServlet(name = "Add", urlPatterns = {"/Add"})
public class Add extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// REQUEST PRODUCT
String productName = request.getParameter("productName").toUpperCase();
int productQuantity = Integer.parseInt(request.getParameter("productQuantity"));
// NEW PRODUCT
Product newProduct = new Product(productName, productQuantity);
}
}
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 Page</title>
</head>
<body>
<div>
<h1><strong>Lista de Compras</strong></h1>
<hr>
<div></div>
</div>
<div>
<h1>INCLUSÃO DE UM NOVO ITEM</h1>
<form action="/add" method="POST" style="background-color: dimgray; padding: 10px;">
<div style="padding: 5px">
<label style="color: white;"><strong>Item:</strong></label>
<input name="productName" type="text" required></input>
</div>
<hr>
<br>
<div>
<label style="color: white;"><strong>Quantidade:</strong></label>
<input name="productQuantity" type="number" min="1" required></input
</div>
<hr>
<div style="padding-top: 30px;">
<button type="submit">ADICIONAR</button>
</div>
</form>
</div>
</body>
</html>
java jsp servlets
"How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?" What is the function of the form? What does the form show? What is it posting?
– prasad_
Nov 21 '18 at 2:24
The form's function is to add a new item .. When it clicks the submit button on this form it should go to the servlet to create a new objet add there is an already existing list and return the list information in the JSP
– THIAGO SAAD
Nov 21 '18 at 2:57
1
There are lot of questions you are asking. You have to do some things first (at least try) and look for answers as you do your work. For starters:Compras.java
is incomplete Java code and it doesn't compile. You are expecting someone to write simple Java code for you. What is the idea?
– prasad_
Nov 21 '18 at 3:10
Here is a link to tutorials/examples to writing servlets and jsp. Please look at relevant chapters related to your application to find some answers. Also, you can search the web (Google) with questions like "How do I display a list in my JSP", and you will find quite a few answers already there.
– prasad_
Nov 21 '18 at 4:09
Similarly you can try searching for ""How do I import classes from other packages".
– prasad_
Nov 21 '18 at 4:16
add a comment |
Good night, I'm learning JAVA WEB + JSP + SERVLETS and as I'm new to both I have the following problems:
I am using the MVC architecture to create the models, views and controllers of my application.
My application works as follows:
In the same route I should make CRUD from a shopping list, for this I have two classes in two different packages which are:
Models - Products.java
Services - Shopping.java
In the Products class I use to instantiate a new item with the code, name and quantity of the product, then with the Purchases I use to store a List of these products and be able to use it in my JSP ...
The question is:
1) How do I create a List of these instantiated objects and do the methods of the code below?
2) How do I display this list in my JSP ..
3) How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?
4) How do I import classes from other packages into my servlet?
Compras.java
public class Compras {
private List<Object> listaProdutos = new List<Object>();
public Compras(List<Object> listaProdutos){
this.listaProdutos = listaProdutos;
}
//Adds a new product to the List
private addProduct(){}
//Removes a specific product from the list
private removeSpecificProduct(){}
//Get all products from the list
private getAllProducts(){}
//Get a specific product from the list
private getSpecificProduct(){}
// Take the size of the list
private getListSize(){}
//Change the position of a specific product in the list
private changeSpecificProductPosition(){}
}
Produtos.java
public class Produtos {
private int quantity;
private String name;
public Produtos(int quantity, String name){
this.quantity = quantity;
this.name = name;
}
}
Servlets - Add.java
@WebServlet(name = "Add", urlPatterns = {"/Add"})
public class Add extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// REQUEST PRODUCT
String productName = request.getParameter("productName").toUpperCase();
int productQuantity = Integer.parseInt(request.getParameter("productQuantity"));
// NEW PRODUCT
Product newProduct = new Product(productName, productQuantity);
}
}
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 Page</title>
</head>
<body>
<div>
<h1><strong>Lista de Compras</strong></h1>
<hr>
<div></div>
</div>
<div>
<h1>INCLUSÃO DE UM NOVO ITEM</h1>
<form action="/add" method="POST" style="background-color: dimgray; padding: 10px;">
<div style="padding: 5px">
<label style="color: white;"><strong>Item:</strong></label>
<input name="productName" type="text" required></input>
</div>
<hr>
<br>
<div>
<label style="color: white;"><strong>Quantidade:</strong></label>
<input name="productQuantity" type="number" min="1" required></input
</div>
<hr>
<div style="padding-top: 30px;">
<button type="submit">ADICIONAR</button>
</div>
</form>
</div>
</body>
</html>
java jsp servlets
Good night, I'm learning JAVA WEB + JSP + SERVLETS and as I'm new to both I have the following problems:
I am using the MVC architecture to create the models, views and controllers of my application.
My application works as follows:
In the same route I should make CRUD from a shopping list, for this I have two classes in two different packages which are:
Models - Products.java
Services - Shopping.java
In the Products class I use to instantiate a new item with the code, name and quantity of the product, then with the Purchases I use to store a List of these products and be able to use it in my JSP ...
The question is:
1) How do I create a List of these instantiated objects and do the methods of the code below?
2) How do I display this list in my JSP ..
3) How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?
4) How do I import classes from other packages into my servlet?
Compras.java
public class Compras {
private List<Object> listaProdutos = new List<Object>();
public Compras(List<Object> listaProdutos){
this.listaProdutos = listaProdutos;
}
//Adds a new product to the List
private addProduct(){}
//Removes a specific product from the list
private removeSpecificProduct(){}
//Get all products from the list
private getAllProducts(){}
//Get a specific product from the list
private getSpecificProduct(){}
// Take the size of the list
private getListSize(){}
//Change the position of a specific product in the list
private changeSpecificProductPosition(){}
}
Produtos.java
public class Produtos {
private int quantity;
private String name;
public Produtos(int quantity, String name){
this.quantity = quantity;
this.name = name;
}
}
Servlets - Add.java
@WebServlet(name = "Add", urlPatterns = {"/Add"})
public class Add extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// REQUEST PRODUCT
String productName = request.getParameter("productName").toUpperCase();
int productQuantity = Integer.parseInt(request.getParameter("productQuantity"));
// NEW PRODUCT
Product newProduct = new Product(productName, productQuantity);
}
}
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 Page</title>
</head>
<body>
<div>
<h1><strong>Lista de Compras</strong></h1>
<hr>
<div></div>
</div>
<div>
<h1>INCLUSÃO DE UM NOVO ITEM</h1>
<form action="/add" method="POST" style="background-color: dimgray; padding: 10px;">
<div style="padding: 5px">
<label style="color: white;"><strong>Item:</strong></label>
<input name="productName" type="text" required></input>
</div>
<hr>
<br>
<div>
<label style="color: white;"><strong>Quantidade:</strong></label>
<input name="productQuantity" type="number" min="1" required></input
</div>
<hr>
<div style="padding-top: 30px;">
<button type="submit">ADICIONAR</button>
</div>
</form>
</div>
</body>
</html>
java jsp servlets
java jsp servlets
asked Nov 21 '18 at 1:49
THIAGO SAAD
1589
1589
"How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?" What is the function of the form? What does the form show? What is it posting?
– prasad_
Nov 21 '18 at 2:24
The form's function is to add a new item .. When it clicks the submit button on this form it should go to the servlet to create a new objet add there is an already existing list and return the list information in the JSP
– THIAGO SAAD
Nov 21 '18 at 2:57
1
There are lot of questions you are asking. You have to do some things first (at least try) and look for answers as you do your work. For starters:Compras.java
is incomplete Java code and it doesn't compile. You are expecting someone to write simple Java code for you. What is the idea?
– prasad_
Nov 21 '18 at 3:10
Here is a link to tutorials/examples to writing servlets and jsp. Please look at relevant chapters related to your application to find some answers. Also, you can search the web (Google) with questions like "How do I display a list in my JSP", and you will find quite a few answers already there.
– prasad_
Nov 21 '18 at 4:09
Similarly you can try searching for ""How do I import classes from other packages".
– prasad_
Nov 21 '18 at 4:16
add a comment |
"How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?" What is the function of the form? What does the form show? What is it posting?
– prasad_
Nov 21 '18 at 2:24
The form's function is to add a new item .. When it clicks the submit button on this form it should go to the servlet to create a new objet add there is an already existing list and return the list information in the JSP
– THIAGO SAAD
Nov 21 '18 at 2:57
1
There are lot of questions you are asking. You have to do some things first (at least try) and look for answers as you do your work. For starters:Compras.java
is incomplete Java code and it doesn't compile. You are expecting someone to write simple Java code for you. What is the idea?
– prasad_
Nov 21 '18 at 3:10
Here is a link to tutorials/examples to writing servlets and jsp. Please look at relevant chapters related to your application to find some answers. Also, you can search the web (Google) with questions like "How do I display a list in my JSP", and you will find quite a few answers already there.
– prasad_
Nov 21 '18 at 4:09
Similarly you can try searching for ""How do I import classes from other packages".
– prasad_
Nov 21 '18 at 4:16
"How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?" What is the function of the form? What does the form show? What is it posting?
– prasad_
Nov 21 '18 at 2:24
"How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?" What is the function of the form? What does the form show? What is it posting?
– prasad_
Nov 21 '18 at 2:24
The form's function is to add a new item .. When it clicks the submit button on this form it should go to the servlet to create a new objet add there is an already existing list and return the list information in the JSP
– THIAGO SAAD
Nov 21 '18 at 2:57
The form's function is to add a new item .. When it clicks the submit button on this form it should go to the servlet to create a new objet add there is an already existing list and return the list information in the JSP
– THIAGO SAAD
Nov 21 '18 at 2:57
1
1
There are lot of questions you are asking. You have to do some things first (at least try) and look for answers as you do your work. For starters:
Compras.java
is incomplete Java code and it doesn't compile. You are expecting someone to write simple Java code for you. What is the idea?– prasad_
Nov 21 '18 at 3:10
There are lot of questions you are asking. You have to do some things first (at least try) and look for answers as you do your work. For starters:
Compras.java
is incomplete Java code and it doesn't compile. You are expecting someone to write simple Java code for you. What is the idea?– prasad_
Nov 21 '18 at 3:10
Here is a link to tutorials/examples to writing servlets and jsp. Please look at relevant chapters related to your application to find some answers. Also, you can search the web (Google) with questions like "How do I display a list in my JSP", and you will find quite a few answers already there.
– prasad_
Nov 21 '18 at 4:09
Here is a link to tutorials/examples to writing servlets and jsp. Please look at relevant chapters related to your application to find some answers. Also, you can search the web (Google) with questions like "How do I display a list in my JSP", and you will find quite a few answers already there.
– prasad_
Nov 21 '18 at 4:09
Similarly you can try searching for ""How do I import classes from other packages".
– prasad_
Nov 21 '18 at 4:16
Similarly you can try searching for ""How do I import classes from other packages".
– prasad_
Nov 21 '18 at 4:16
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404224%2fhow-to-create-a-class-to-store-an-object-list-and-display-in-the-jsp-without-del%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404224%2fhow-to-create-a-class-to-store-an-object-list-and-display-in-the-jsp-without-del%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"How do I do the POST of the form without updating the page, and at the same time my list of products is not erased?" What is the function of the form? What does the form show? What is it posting?
– prasad_
Nov 21 '18 at 2:24
The form's function is to add a new item .. When it clicks the submit button on this form it should go to the servlet to create a new objet add there is an already existing list and return the list information in the JSP
– THIAGO SAAD
Nov 21 '18 at 2:57
1
There are lot of questions you are asking. You have to do some things first (at least try) and look for answers as you do your work. For starters:
Compras.java
is incomplete Java code and it doesn't compile. You are expecting someone to write simple Java code for you. What is the idea?– prasad_
Nov 21 '18 at 3:10
Here is a link to tutorials/examples to writing servlets and jsp. Please look at relevant chapters related to your application to find some answers. Also, you can search the web (Google) with questions like "How do I display a list in my JSP", and you will find quite a few answers already there.
– prasad_
Nov 21 '18 at 4:09
Similarly you can try searching for ""How do I import classes from other packages".
– prasad_
Nov 21 '18 at 4:16