• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP code to complete a task  RSS feed

 
Rebecca Green
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

So glad to find this forum...
I am totally new to servlets and JSP and started to learn from the Head First series a few days ago.
But my teacher gave an assignment in JSP which I have no clue how to do.

Can anyone please help me out??

The assignment is this...

A JSP code to create Employee details with the following fields :
1. Name
2. Salary
3.Employee ID

It should also have options to Add and Modify.
And once the form is filled and saved, a page containing the details of all employees including the newly added one must be displayed.

I am supposed to store it in a database and also be able to retrieve it.

I guess some of you might laugh at me as this appears simple... but I'm a total fresher at this stuff... I need your help people!!
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly are you supposed to solve your task ?

The general approach is to have a servlet do the work (your fields go into a java bean) and the JSP only display the results.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far?
 
Rebecca Green
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:How exactly are you supposed to solve your task ?

The general approach is to have a servlet do the work (your fields go into a java bean) and the JSP only display the results.


Yeah... I also need to have a servlet which does the work...
And I am going through several examples but have not started to write anything...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rebecca,
As a suggestion to start, try breaking it up into smaller pieces.

Can you:
  • Write a class/method that inserts an Employee into the database when passed those three values as Strings?
  • Can you create a Hello World JSP?
  • Can you create a JSP with a form?
  • Can you create a servlet that reads three parameters?
  • Can you hook together the whole thing?

  •  
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Rebecca, it is simple, I will be explaining you how to do that.

    step 1: code a html page which asks the user to enter his or details.

    step 2: write a servlet which implements the doGet() or doPost() method based on the method you used in the html form. note: if you use post method in your html form and implement doGet() method in the servlet then you will get an error. So be cautious.

    step 3: In the controller get the values sent from the html page. Now you have to create a model which actually stores the data in the database.

    step 4: the controller is responsible for calling the model, the controller passes the values received from the html page to the model. the model saves the data.

    step 5: the model is responsible for connecting to the database.

    step 6: after the model executes successfully, now you have to call a jsp page from the controller to display the data you have stored in the database.

    step 7: save the sevlet class file in WEB-INF classes folder.

    step 8: In the deployment descriptor map the servlet and html page.

    Your project structure must look like this.



    Hope you understood, have a nice day

    If I am wrong anywhere please let me know.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66144
    144
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    anusha chaitanya wrote:If I am wrong anywhere please let me know.

    One problem I see is that all classes involved with a web application should be in a package other than the default.
     
    David Newton
    Author
    Rancher
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And there's a source file deployed.

    (And I'm not convinced the model should be what does the DB access, but that's a different type of issue.)
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66144
    144
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ... and the JSP should be hidden behind WEB-INF to prevent direct access.
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:... and the JSP should be hidden behind WEB-INF to prevent direct access.


    Thanks Bear for those suggestions, as she is a beginner I proposed her that architecture.
     
    David Newton
    Author
    Rancher
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But it won't run if the servlet is in the default package...
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66144
    144
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    anusha chaitanya wrote:Thanks Bear for those suggestions, as she is a beginner I proposed her that architecture.

    Making poor practice suggestions, especially to a beginner, is not a good idea. It instills bad habits that must be unlearned later.
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Making poor practice suggestions, especially to a beginner, is not a good idea. It instills bad habits that must be unlearned later.

    Thank you Bear, next time I ll try my level best to help others.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66144
    144
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    anusha chaitanya wrote:I ll try my level best to help others.

    I like your attitude!
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:
    anusha chaitanya wrote:I ll try my level best to help others.

    I like your attitude!


    Thank you sir.
     
    Rebecca Green
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I finished the first part of the assignment... the add option... thanks guys
    Your answers helped a lot. But I still need to do a lot of work and add a 2 more options - Modify and Delete. I did that to some extent, but its not working!
    I will paste my code here. please tell me where the error might be...
    I have taken a seperate servletfor deleting called DeleteDb.
    I have placed a 'Delete' button in the jsp page which is used to retrieve the table from the database.

    In the jsp page, I have added checkboxes and have placed a delete button such that upon clicking, it goes to the DeleteDB servlet :



    Here is the DeleteDB servlet code :


    please point out what might have gone wrong. Also, what all changes shoyld I make to this code in order to implement 'Modify' option...

    Thanks
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the if else is not placed properly. what is the error you are getting

     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And one more thing is I think you are selecting multiple checkboxes and clicking the delete button. If I am right, then for that kind of job this logic is not correct. Use this one.
     
    Rebecca Green
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Its not placed peoperly??
    Can you elaborate it a little more...??

    And the 'else' part is displaying once i click on the 'Delete' button... i mean, "Error while deleting" is being displayed.
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rebecca Green wrote:Its not placed peoperly??
    Can you elaborate it a little more...??

    And the 'else' part is displaying once i click on the 'Delete' button... i mean, "Error while deleting" is being displayed.


    The if-else statement is not placed properly.
    it is not


    It is

     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rebecca Green wrote:

    And the 'else' part is displaying once i click on the 'Delete' button... i mean, "Error while deleting" is being displayed.


    since you are dispatching the request to the next page you are unable to see the "Error while deleting" statement
     
    Rebecca Green
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    anusha chaitanya wrote:And one more thing is I think you are selecting multiple checkboxes and clicking the delete button. If I am right, then for that kind of job this logic is not correct. Use this one.


    I tried this... now i'm getting "java.lang.NullPointerException " once i click on the delete button
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can you paste all the pages, or send it to my mail id.
     
    Rebecca Green
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sure... will send it right away

    Thanks
     
    Rebecca Green
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I sent it to the mail id mentioned in your profile... is that correct...??
     
    chaitanya karthikk
    Ranch Hand
    Posts: 806
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rebecca Green wrote:I sent it to the mail id mentioned in your profile... is that correct...??


    yes
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66144
    144
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rebecca and anusha, please use the forum not email.

    Please read this for more information.
     
    Rebecca Green
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Problem solved!! Thanks Anusha!!
    The problem was a small one. I had to place the <form> tag inside the while loop and thats it



    This was the change made. If anyone wants teh complete code, feel free to PM me.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!