如何在Java Servlet中创建类对象数组

时间:2019-03-25 10:38:12

标签: java html object servlets dynamic-websites

我收到了一个涉及servlet页面和html的作业,我们必须在其中记录电影院座位/预订html页面的用户预订数据。对于糟糕的编码,我感到抱歉,我已经有两年没有上软件课了,这对我来说是一种新的语言。建议我们有一个servlet页面,它充当用户数据的数据库,该页面创建了一个Booker对象,该对象存储在数据库中。几年前,我有一个电影数据库Java项目,它使用了整个数据库类和对象类,因此我只是从那里采样了我的代码,并尝试将servlet与数据库类集成在一起。我什至不确定这是否是一种有效的方法,当我们问他有关作业的问题时,我们的讲师非常模棱两可。任何帮助,将不胜感激。让我知道您是否需要该项目的更多资源/ java / html文件。

Servlet /数据库(所有到booker对象类的东西)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;


    @WebServlet(urlPatterns = {"/CustomServlet"})
public class CustomServlet extends HttpServlet {

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

    response.setContentType("text/html");



    private Booker[] booker;
    public int total;
    private int MAX = 63;

    String id = request.getParameter("id");
    String phone = request.getParameter("phone");
    String address = request.getParameter("address");
    String email = request.getParameter("email");
    String secCode = request.getParameter("secCode");


    //counts how many movies are in the system and creates array to store movie data with max length originally 4
    public BookerDatabase()
    {
        booker = new Booker[MAX];
        total = 0;
    }


    addBookerData(id,phone,address,email,secCode);


    public void addBookerData(String id, String phone, String address, String email, String secCode)
    {
        booker[total] = new Booker();
        setData(booker[total], id, phone, address, email, secCode);
        total++;
    }

private void setData(Booker b, String id, String phone, String address, String email, String secCode) 
    {
         b.setID(id);
         b.setPhone(phone);
         b.setAddress(address);
         b.setEmail(email);
         b.setSecCode(secCode);
    }


        PrintWriter out = response.getWriter();

        try{
            out.println("<!DOCTYPE html>");
            out.println("<html><head>");
            out.println("<title>Custom Servlet</title></head>");
            out.println("<body>");
            out.println("<h1>ID = " + Booker[0].getID());
            out.println("<br>");
            out.println("<h1>Ph = " + Booker[0].getPhone());
            out.println("<br>");
            out.println("<h1>Ad = " + Booker[0].getAddress());
            out.println("<br>");
            out.println("<h1>Em = " + Booker[0].getEmail());
            out.println("<br>");
            out.println("<h1>SC = " + Booker[0].getSecCode());
            out.println("<br></h1>");
            out.println("</body>");
            out.println("</html>");

        }
        finally{
            out.close();
        }

    } 
}

Booker对象类

public class Booker
{
    private String id, phone, address, email, secCode; 




    public Booker()
    {
        id = "";
        phone = "";
        address = "";
        email = "";
        secCode = "";

    }


    public void setID(String newId)
    {
        id = newId;
    }

    public String getID()
    {
        return id;
    }


    public void setPhone(String newPhone)
    {
        phone = newPhone;
    }

    public String getPhone()
    {
        return phone;
    }


    public void setAddress(String newAddress)
    {
        address = newAddress;
    }

    public String getAddress()
    {
        return address;
    }


    public void setEmail(String newEmail)
    {
        email = newEmail;
    }

    public String getEmail()
    {
        return email;
    }

    public void setSecCode(String newSecCode)
    {
        secCode = newSecCode;
    }

    public String getSecCode()
    {
        return secCode;
    }
}

0 个答案:

没有答案