I have a program that displays a list of vaccines and for each vaccine listed in the table, you can edit them. When I click on edit for the specified vaccine, one of the values allowed to be modified is the number of doses required, which can be changed from a drop down list. I'm having trouble being able to pass that value selected to the doPost method, as when I try changing, it goes back to the main page and shows the value for doses required as null. How can I fix this?

Here's the main vaccine list servlet:

package vaccineList.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import NewVaccine.servlet.NewVaccine;
import NewDose.servlet.NewDose;

//import cs3220.model.GuestBookEntry;
//import cs3220.utilities.*;

@WebServlet(urlPatterns = "/ListVaccine", loadOnStartup = 1)
public class ListVaccine extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public ListVaccine()
    {
        super();
    }

    public void init( ServletConfig config ) throws ServletException
    {
        super.init( config );

        List<VaccineEntry> entries = new ArrayList<VaccineEntry>();
        entries.add( new VaccineEntry( "Pfizer/BioNTech", "2", "21", "10000", "10000" ) );
        entries.add( new VaccineEntry( "Johnson&Johnson", "1", "0", "5000", "5000"  ) );

        getServletContext().setAttribute( ServletFunctions.VaccineEntries, entries );
    }

    @SuppressWarnings("unchecked")
    protected void doGet( HttpServletRequest request,
        HttpServletResponse response ) throws ServletException, IOException
    {
        StringBuilder html = new StringBuilder();
        List<VaccineEntry> entries = ServletFunctions.getVaccineEntriesFromServletContext(getServletContext());

        response.setContentType( "text/html" );
        PrintWriter out = response.getWriter();

        html.append( "<p>List Vaccine</p>" );

        html.append( "<p><a href='NewVaccine'>New Vaccine</a> |" );
        html.append( "<a href='NewDose'> New Dose</a></p>" );

        html.append("<table border='1'>");

        html.append("<tr>");
        html.append( "<td>   " + " <b>Vaccine</b>   </td>" );
        html.append( "<td>" + " <b>Doses Required</b></td>" );
        html.append( "<td>" + " <b>Days Between Doses</b></td>" );
        html.append( "<td>" + " <b>Total Doses Recieved</b></td>" );
        html.append( "<td>" + " <b>Total Doses Left</b></td>" );
        html.append("<td></td>" );
        html.append( "</tr>" );

        for( VaccineEntry entry : entries )
        {

            html.append("<tr>");
            html.append( "<td>" + entry.getName() + "</td>" );
            html.append( "<td>" + entry.getDosesReq() + "</td>" );
            if(entry.getDaysBtwnDoses() == "0") {
                html.append( "<td></td>" );
            }
            else
                html.append( "<td>" + entry.getDaysBtwnDoses() + "</td>" );


            html.append( "<td>" + entry.getTotalDosesRecieved() + "</td>" );
            html.append( "<td>" + entry.getTotalDosesLeft() + "</td>" );


            html.append( "<td><a href='editVaccine?id=" + entry.getId() + "&dosesReq=" + entry.getDosesReq() + "'>Edit</a></td>"  );


            html.append( "</td>" );
            html.append( "</tr>" );
        }
        html.append( "</table>" );



        out.println( ServletFunctions.titleWithBody("ListVaccine", html.toString()) );
    }

}

and here's the edit vaccine page:

package EditVaccine.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import vaccineList.servlet.*;


@WebServlet("/editVaccine")
public class editVaccine extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public editVaccine() {
        super();

    }

    @SuppressWarnings("unchecked")
    private VaccineEntry getEntry( int id )
    {
        List<VaccineEntry> entries = (List<VaccineEntry>) getServletContext()
            .getAttribute( "entries" );
        System.out.println("entry taken is " + id);


        for( VaccineEntry entry : entries )
            if( entry.getId() == id ) return entry;
        return null;
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        StringBuilder html = new StringBuilder();

        String id = request.getParameter( "id" );
        String dosesReq = request.getParameter("dosesReq");
        VaccineEntry entry = getEntry(Integer.parseInt(id));
        System.out.println("The value of id is " + id);
        System.out.println("The value of dosesReq is " + dosesReq);


        System.out.println("The value of getEntry is " + entry.getId());

        response.setContentType( "text/html" );
        PrintWriter out = response.getWriter();

        html.append("<form action='editVaccine' method='post'>" );
        html.append("<table border='1' cellpadding='2'>");
        html.append("<tr>" + "<th colspan='2'>Name</th>");
        html.append("<input type='hidden' value='" + id + "' name='id' id='id' />");
        html.append("<input type='hidden' value='" + dosesReq + "' name='dosesReq' id='dosesReq' />");
        html.append("<td><input type='text' name='name' value=" + entry.getName() + "></input></td>" + "</tr>");
        html.append("<tr><th colspan='2'>Doses Required</th>" + "<td><select id='dosesReq' name='dosesReq' " + "'>"); 
        html.append("<option value='1'" + "'>1</option>");
        html.append("<option value='2'" + "'>2</option>");
        html.append("<option value='3'" + "'>3</option>");
        html.append("</select></td>");
        html.append("<tr><th colspan='2'>Days Between Doses</th><td><input type='text' name= 'DaysBtwnDoses' value='" + entry.getDaysBtwnDoses() + "'> </input></td></tr>");
        html.append("<td colspan = '2'><button>Save</button></td>");
        html.append("</form>");
        html.append("</table>");


        out.println( ServletFunctions.titleWithBody("ListVaccine", html.toString()) );

       String[] dropDown = request.getParameterValues("dosesReq");
       System.out.println("dropdown:" + dropDown);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        VaccineEntry entry = getEntry(Integer.parseInt(request.getParameter("id")));

        entry.setName(request.getParameter("name"));
        entry.setDosesReq(request.getParameter("dosesReq"));
        String name = entry.getName();
        System.out.println("name is " + name);
        entry.setDosesReq(request.getParameter("dosage"));
        String dosage = entry.getDosesReq();
        System.out.println("dosage is " + dosage);
        entry.setDaysBtwnDoses(request.getParameter("DaysBtwnDoses"));
        String DaysBtwnDoses = entry.getDaysBtwnDoses();
        System.out.println("DaysBtwnDoses is " + DaysBtwnDoses);

        response.sendRedirect("ListVaccine");
        return;

    }

}

You have too much code, that is why you don't get answers, it a bit hard to follow, rather point straight to the portion of codes that should be doing what you are trying to do, like just show that function with fewer codes for better answers. Make 1 example of the actual data from your source and how you want it to be and the code you tried to achieve that so that it will be easy to see what's wrong with your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.