Student
Published: 2023-03-14 02:50:33

Index. Html

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="result">
            <p>STUDENT ID:<input type="text" name="id"></p>
            <p>STUDENT NAME:<input type="text" name="name"></p>
            <p>MATHS:<input type="text" name="maths"></p>
            <p>SCIENCE:<input type="text" name="sci"></p>
            <p>ENGLISH:<input type="text" name="eng"></p>
            <p>COMPUTER:<input type="text" name="comp"></p>
            <p><input type="submit" name="submit" value="SUBMIT">
               
        </form>
    </body>
</html>

Student
package student;

/**
 *
 * @author rampu
 */
public class student {
    String studentName;
    int studentId;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public student(String studentName, int studentId) {
        this.studentName = studentName;
        this.studentId = studentId;
    }
    
}

Marks
package student;

/**
 *
 * @author rampu
 */
public class marks extends student{
    int sci;
    int maths;
    int eng;
    int comp;
    int total;
    float per;

    public marks(String studentName, int studentId,int sci, int maths, int eng, int comp) {
        super(studentName, studentId);
        this.sci = sci;
        this.maths = maths;
        this.eng = eng;
        this.comp = comp;
    }
       

    public marks(String studentName, int studentId) {
        super(studentName, studentId);
    }
    public int total(){
    this.total=this.sci+this.maths+this.comp+this.eng;
    return this.total;
    }
    public float per(){
    this.per=(this.total*100)/(100*4);
    return this.per;
    }
    
}
Servlet

import student.marks;
import student.student;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author rampu
 */
public class result extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            int id=Integer.parseInt(request.getParameter("id"));
            String name=request.getParameter("name");
            int maths=Integer.parseInt(request.getParameter("maths"));
            int sci=Integer.parseInt(request.getParameter("sci"));
            int eng=Integer.parseInt(request.getParameter("eng"));
            int comp=Integer.parseInt(request.getParameter("comp"));
             marks m1=new marks(name,id,sci,maths,comp,eng);
            int total=m1.total();
            float per=m1.per();
             Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/result", "root", "");
            Statement stmt1= conn.createStatement();
            stmt1.executeUpdate("insert into result values('"+total+"','"+per+"')");
            Statement stmt2= conn.createStatement();
            stmt2.executeUpdate("insert into marks values('"+maths+"','"+sci+"','"+eng+"','"+comp+"')");
             Statement stmt3= conn.createStatement();
             stmt1.close();
             stmt2.close();
             
            stmt3.executeUpdate("insert into student values('"+id+"','"+name+"')");
            stmt3.close();
            conn.close();
            out.println("</td></tr>" + "<table border='5px black ' style='font-size:30px;'><tr><th><b> Enrollment Number</b></th><td>"+id+"</td></tr><tr><th><b>Student Name</b></th><td>"+name+"</td></tr>"
                    + "<tr><th>Maths</th><td>"+maths+"</td></tr>"+ "<tr><th>Science</th><td>"+sci+"</td></tr>"+ "<tr><th>English</th><td>"+eng+"</td></tr>"+ "<tr><th>Computer</th><td>"+comp+"</td></tr>"
                            +"<tr><th><b> Total</b></th><td>"+total+"</td></tr><tr><th><b>Percentage</b></th><td>"+per+"</td></tr></table>");
                         
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(result.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(result.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(result.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(result.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}