મુખ્ય સામગ્રી પર જાઓ
  1. સંસાધનો/
  2. અભ્યાસ સામગ્રી/
  3. ઇન્ફોર્મેશન ટેકનોલોજી એન્જિનિયરિંગ/
  4. આઈટી સેમેસ્ટર 5/
  5. એડવાન્સ જાવા પ્રોગ્રામિંગ (4351603)/

એડવાન્સ જાવા પ્રોગ્રામિંગ (4351603) - ઉનાળા 2024 સોલ્યુશન

·
અભ્યાસ-સામગ્રી સોલ્યુશન એડવાન્સ-જાવા 4351603 2024 ઉનાળા
મિલવ ડબગર
લેખક
મિલવ ડબગર
ઇલેક્ટ્રિકલ અને ઇલેક્ટ્રોનિક મેન્યુફેક્ચરિંગ ઉદ્યોગમાં અનુભવી લેક્ચરર. એમ્બેડેડ સિસ્ટમ્સ, ઈમેજ પ્રોસેસિંગ, ડેટા સાયન્સ, મેટલેબ, પાયથન, STM32માં કુશળ. એલ.ડી. કોલેજ ઓફ એન્જિનિયરિંગ - અમદાવાદથી કમ્યુનિકેશન સિસ્ટમ્સ એન્જિનિયરિંગમાં માસ્ટર્સ ડિગ્રી ધરાવતા મજબૂત શિક્ષણ વ્યાવસાયિક.
અનુક્રમણિકા

પ્રશ્ન 1(અ) [3 ગુણ]
#

AWT અને Swing વચ્ચેનો તફાવત સમજાવો.

જવાબ:

લક્ષણAWTSwing
PlatformPlatform dependentPlatform independent
ComponentsHeavy weightLight weight
Look & FeelNative OS lookPluggable look & feel
PerformanceઝડપીAWT કરતાં ધીમું

મુખ્ય મુદ્દાઓ:

  • Heavy vs Light: AWT native OS components વાપરે છે, Swing pure Java વાપરે છે
  • દેખાવ: AWT OS style અનુસરે છે, Swing બધા platforms પર સમાન look આપે છે
  • સુવિધાઓ: Swing વધુ advanced components જેમ કે JTable, JTree પ્રદાન કરે છે

મેમરી ટ્રીક: “Swing Provides Lightweight Components”


પ્રશ્ન 1(બ) [4 ગુણ]
#

Mouse Motion Listener ને ઉદાહરણ સાથે સમજાવો.

જવાબ:

MouseMotionListener interface Java Swing applications માં mouse movement events ને handle કરે છે.

કોષ્ટક: Mouse Motion Events

Methodહેતુ
mouseDragged()જ્યારે mouse drag થાય ત્યારે call થાય
mouseMoved()જ્યારે mouse ખસે ત્યારે call થાય

કોડ ઉદાહરણ:

import javax.swing.*;
import java.awt.event.*;

class MouseMotionExample extends JFrame implements MouseMotionListener {
    JLabel label;
    
    MouseMotionExample() {
        label = new JLabel("અહીં mouse ખસાડો");
        add(label);
        addMouseMotionListener(this);
        setSize(400, 300);
        setVisible(true);
    }
    
    public void mouseMoved(MouseEvent e) {
        label.setText("Mouse આ સ્થાને: " + e.getX() + ", " + e.getY());
    }
    
    public void mouseDragged(MouseEvent e) {
        label.setText("Dragging આ સ્થાને: " + e.getX() + ", " + e.getY());
    }
}

મેમરી ટ્રીક: “Mouse Motion Makes Dynamic”


પ્રશ્ન 1(ક) [7 ગુણ]
#

યુનિવર્સિટી સાથે જોડાયેલા વિવિધ અભ્યાસક્રમો માટે checkboxes બનાવવા માટે એક પ્રોગ્રામ ડેવલપ કરો જેથી પસંદ કરેલ કોર્સ પ્રદર્શિત થાય.

જવાબ:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CourseSelection extends JFrame implements ItemListener {
    JCheckBox java, python, cpp, web;
    JTextArea display;
    
    public CourseSelection() {
        setTitle("યુનિવર્સિટી કોર્સ પસંદગી");
        setLayout(new FlowLayout());
        
        // checkboxes બનાવો
        java = new JCheckBox("Java Programming");
        python = new JCheckBox("Python Programming");
        cpp = new JCheckBox("C++ Programming");
        web = new JCheckBox("Web Development");
        
        // listeners ઉમેરો
        java.addItemListener(this);
        python.addItemListener(this);
        cpp.addItemListener(this);
        web.addItemListener(this);
        
        // Display area
        display = new JTextArea(10, 30);
        display.setEditable(false);
        
        // components ઉમેરો
        add(new JLabel("કોર્સ પસંદ કરો:"));
        add(java); add(python); add(cpp); add(web);
        add(new JScrollPane(display));
        
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    public void itemStateChanged(ItemEvent e) {
        String courses = "પસંદ કરેલ કોર્સ:\n";
        if(java.isSelected()) courses += "- Java Programming\n";
        if(python.isSelected()) courses += "- Python Programming\n";
        if(cpp.isSelected()) courses += "- C++ Programming\n";
        if(web.isSelected()) courses += "- Web Development\n";
        display.setText(courses);
    }
    
    public static void main(String[] args) {
        new CourseSelection();
    }
}

મુખ્ય લક્ષણો:

  • ItemListener: checkbox state changes ને detect કરે છે
  • Dynamic Display: real-time માં પસંદ કરેલા કોર્સ update કરે છે
  • Multiple Selection: એકથી વધુ કોર્સ પસંદ કરવાની મંજૂરી આપે છે

મેમરી ટ્રીક: “Check Items Listen Dynamically”


પ્રશ્ન 1(ક) અથવા [7 ગુણ]
#

Swing components નો ઉપયોગ કરીને (JFrame, JRadioButton, ItemListener વગેરેનો ઉપયોગ કરીને) Traffic signal (લાલ, લીલો અને પીળો) implement કરવા માટે એક પ્રોગ્રામ વિકસાવો.

જવાબ:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TrafficSignal extends JFrame implements ItemListener {
    JRadioButton red, green, yellow;
    ButtonGroup group;
    JPanel signalPanel;
    
    public TrafficSignal() {
        setTitle("Traffic Signal સિમ્યુલેટર");
        setLayout(new BorderLayout());
        
        // radio buttons બનાવો
        red = new JRadioButton("લાલ");
        green = new JRadioButton("લીલો"); 
        yellow = new JRadioButton("પીળો");
        
        // radio buttons ને group કરો
        group = new ButtonGroup();
        group.add(red); group.add(green); group.add(yellow);
        
        // listeners ઉમેરો
        red.addItemListener(this);
        green.addItemListener(this);
        yellow.addItemListener(this);
        
        // Signal display panel
        signalPanel = new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLACK);
                g.fillRect(50, 50, 100, 200);
                
                // વર્તુળો દોરો
                g.setColor(red.isSelected() ? Color.RED : Color.GRAY);
                g.fillOval(65, 65, 70, 70);
                
                g.setColor(yellow.isSelected() ? Color.YELLOW : Color.GRAY);
                g.fillOval(65, 105, 70, 70);
                
                g.setColor(green.isSelected() ? Color.GREEN : Color.GRAY);
                g.fillOval(65, 145, 70, 70);
            }
        };
        
        JPanel controlPanel = new JPanel();
        controlPanel.add(red); controlPanel.add(yellow); controlPanel.add(green);
        
        add(controlPanel, BorderLayout.SOUTH);
        add(signalPanel, BorderLayout.CENTER);
        
        setSize(300, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    public void itemStateChanged(ItemEvent e) {
        signalPanel.repaint();
    }
    
    public static void main(String[] args) {
        new TrafficSignal();
    }
}

આકૃતિ:

[TRr]afY[fEGYiRLR]cELEDOE[BWNGo]x

મેમરી ટ્રીક: “Radio Buttons Paint Graphics”


પ્રશ્ન 2(અ) [3 ગુણ]
#

JDBC Type-4 driver સમજાવો.

જવાબ:

JDBC Type-4 Driver (Native Protocol Driver)

લક્ષણવર્ણન
પ્રકારPure Java driver
CommunicationDirect database protocol
PlatformPlatform independent
Performanceસર્વોચ્ચ પ્રદર્શન

મુખ્ય મુદ્દાઓ:

  • Pure Java: કોઈ native code ની જરૂર નથી
  • Direct Connection: ડેટાબેઝ સાથે સીધો સંપર્ક કરે છે
  • Network Protocol: ડેટાબેઝના native network protocol નો ઉપયોગ કરે છે
  • શ્રેષ્ઠ પ્રદર્શન: બધા driver types માં સૌથી ઝડપી

મેમરી ટ્રીક: “Pure Java Direct Protocol”


પ્રશ્ન 2(બ) [4 ગુણ]
#

Component class ની સામાન્ય રીતે વપરાતી methods સમજાવો.

જવાબ:

કોષ્ટક: Component Class Methods

Methodહેતુ
add()container માં component ઉમેરે છે
setSize()component ના dimensions સેટ કરે છે
setLayout()layout manager સેટ કરે છે
setVisible()component ને દૃશ્યમાન/અદૃશ્ય બનાવે છે
setBounds()position અને size સેટ કરે છે
getSize()component નું size return કરે છે

મુખ્ય લક્ષણો:

  • Layout Management: component arrangement ને control કરે છે
  • Visibility Control: components ને દેખાડે/છુપાવે છે
  • Size Management: component dimensions ને control કરે છે
  • Container Operations: child components ને manage કરે છે

મેમરી ટ્રીક: “Add Set Get Visibility”


પ્રશ્ન 2(ક) [7 ગુણ]
#

ટેબલ ‘StuRec’ માંથી વિદ્યાર્થીના રેકોર્ડ (Enroll No, Name, Address, Mobile No અને Email-ID) દર્શાવવા માટે JDBC નો ઉપયોગ કરીને પ્રોગ્રામ વિકસાવો.

જવાબ:

import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class StudentRecordDisplay extends JFrame {
    JTable table;
    DefaultTableModel model;
    
    public StudentRecordDisplay() {
        setTitle("વિદ્યાર્થી રેકોર્ડ્સ");
        
        // table model બનાવો
        String[] columns = {"Enroll No", "Name", "Address", "Mobile", "Email"};
        model = new DefaultTableModel(columns, 0);
        table = new JTable(model);
        
        // ડેટા લોડ કરો
        loadStudentData();
        
        add(new JScrollPane(table));
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    private void loadStudentData() {
        try {
            // ડેટાબેઝ કનેક્શન
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/university", "root", "password");
            
            // query execute કરો
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM StuRec");
            
            // table માં ડેટા ઉમેરો
            while(rs.next()) {
                String[] row = {
                    rs.getString("enrollno"),
                    rs.getString("name"),
                    rs.getString("address"),
                    rs.getString("mobile"),
                    rs.getString("email")
                };
                model.addRow(row);
            }
            
            con.close();
        } catch(Exception e) {
            JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
        }
    }
    
    public static void main(String[] args) {
        new StudentRecordDisplay();
    }
}

ડેટાબેઝ ટેબલ માળખું:

CREATE TABLE StuRec (
    enrollno VARCHAR(20) PRIMARY KEY,
    name VARCHAR(50),
    address VARCHAR(100),
    mobile VARCHAR(15),
    email VARCHAR(50)
);

મેમરી ટ્રીક: “Connect Query Display Records”


પ્રશ્ન 2(અ) અથવા [3 ગુણ]
#

JDBC ના ફાયદા અને ગેરફાયદા લખો.

જવાબ:

કોષ્ટક: JDBC ફાયદા અને ગેરફાયદા

ફાયદાગેરફાયદા
Platform IndependentPerformance Overhead
Database Independentશરૂઆતી લોકો માટે જટિલ
Standard APISQL dependency
Transactions ને support કરેManual resource management

મુખ્ય મુદ્દાઓ:

  • પોર્ટેબિલિટી: વિવિધ platforms અને databases પર કામ કરે છે
  • સ્ટાન્ડર્ડાઇઝેશન: database operations માટે uniform API
  • પ્રદર્શન: વધારાનું layer performance માં overhead લાવે છે
  • જટિલતા: યોગ્ય resource management જરૂરી

મેમરી ટ્રીક: “Platform Independent Standard Complex”


પ્રશ્ન 2(બ) અથવા [4 ગુણ]
#

Border Layout સમજાવો.

જવાબ:

BorderLayout container ને પાંચ વિસ્તારોમાં વહેંચે છે: North, South, East, West, અને Center.

આકૃતિ:

WESTNCSOEORNUTTTHEHREAST

કોષ્ટક: Border Layout વિસ્તારો

વિસ્તારસ્થાનવર્તન
NORTHઉપરPreferred height, full width
SOUTHનીચેPreferred height, full width
EASTજમણેPreferred width, full height
WESTડાબેPreferred width, full height
CENTERવચ્ચેબાકીની જગ્યા લે છે

કોડ ઉદાહરણ:

setLayout(new BorderLayout());
add(new JButton("ઉત્તર"), BorderLayout.NORTH);
add(new JButton("મધ્ય"), BorderLayout.CENTER);

મેમરી ટ્રીક: “North South East West Center”


પ્રશ્ન 2(ક) અથવા [7 ગુણ]
#

Hibernate CRUD operations નો ઉપયોગ કરીને Employee (NAME, AGE, SALARY અને DEPARTMENT) નો ડેટા store, update, fetch અને delete માટે એપ્લિકેશન ડેવલપ કરો.

જવાબ:

Employee Entity Class:

import javax.persistence.*;

@Entity
@Table(name = "employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    private String name;
    private int age;
    private double salary;
    private String department;
    
    // Constructors, getters, setters
    public Employee() {}
    
    public Employee(String name, int age, double salary, String dept) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.department = dept;
    }
    
    // Getters અને Setters
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    // ... અન્ય getters/setters
}

CRUD Operations Class:

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class EmployeeCRUD {
    private SessionFactory factory;
    
    public EmployeeCRUD() {
        factory = new Configuration()
                    .configure("hibernate.cfg.xml")
                    .addAnnotatedClass(Employee.class)
                    .buildSessionFactory();
    }
    
    // CREATE
    public void saveEmployee(Employee emp) {
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(emp);
        tx.commit();
        session.close();
    }
    
    // READ
    public Employee getEmployee(int id) {
        Session session = factory.openSession();
        Employee emp = session.get(Employee.class, id);
        session.close();
        return emp;
    }
    
    // UPDATE
    public void updateEmployee(Employee emp) {
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        session.update(emp);
        tx.commit();
        session.close();
    }
    
    // DELETE
    public void deleteEmployee(int id) {
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        Employee emp = session.get(Employee.class, id);
        session.delete(emp);
        tx.commit();
        session.close();
    }
}

મેમરી ટ્રીક: “Save Get Update Delete Hibernate”


પ્રશ્ન 3(અ) [3 ગુણ]
#

Deployment Descriptor સમજાવો.

જવાબ:

Deployment Descriptor (web.xml) web applications માટે configuration file છે જેમાં servlet mappings, initialization parameters, અને security settings હોય છે.

કોષ્ટક: Deployment Descriptor Elements

Elementહેતુ
<servlet>servlet configuration define કરે છે
<servlet-mapping>servlet ને URL pattern સાથે map કરે છે
<init-param>initialization parameters સેટ કરે છે
<welcome-file-list>default files serve કરવા માટે

મુખ્ય લક્ષણો:

  • Configuration: web app માટે કેન્દ્રીય configuration
  • Servlet Mapping: URL to servlet mapping
  • Parameters: initialization અને context parameters
  • Security: authentication અને authorization settings

મેમરી ટ્રીક: “Web XML Configuration Mapping”


પ્રશ્ન 3(બ) [4 ગુણ]
#

servlet માં get અને post method વચ્ચેનો તફાવત સમજાવો.

જવાબ:

કોષ્ટક: GET vs POST Methods

લક્ષણGETPOST
Data LocationURL query stringRequest body
Data Sizeમર્યાદિત (2048 chars)અમર્યાદિત
Securityઓછું સુરક્ષિત (દૃશ્યમાન)વધુ સુરક્ષિત
CachingCache થઈ શકે છેCache થતું નથી
BookmarkingBookmark કરી શકાયBookmark કરી શકાતું નથી
હેતુડેટા retrieve કરવાડેટા submit/modify કરવા

મુખ્ય મુદ્દાઓ:

  • દૃશ્યતા: GET ડેટા URL માં દેખાય છે, POST છુપાયેલું હોય છે
  • ક્ષમતા: POST મોટો ડેટા handle કરી શકે છે
  • સુરક્ષા: POST sensitive ડેટા માટે વધુ સારી
  • ઉપયોગ: GET fetching માટે, POST form submission માટે

મેમરી ટ્રીક: “GET Visible Limited, POST Hidden Unlimited”


પ્રશ્ન 3(ક) [7 ગુણ]
#

એક સરળ servlet પ્રોગ્રામ વિકસાવો જે તેના લોડિંગ પછી કેટલી વખત તેને access કરવામાં આવ્યું છે તેમાટે counter જાળવી રાખે છે; deployment descriptor નો ઉપયોગ કરીને counter ને પ્રારંભ કરો.

જવાબ:

Servlet કોડ:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CounterServlet extends HttpServlet {
    private int counter;
    
    public void init() throws ServletException {
        String initialValue = getInitParameter("initialCount");
        counter = Integer.parseInt(initialValue);
    }
    
    protected void doGet(HttpServletRequest request, 
                        HttpServletResponse response) 
                        throws ServletException, IOException {
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        synchronized(this) {
            counter++;
        }
        
        out.println("<html><body>");
        out.println("<h2>પેજ Access કાઉન્ટર</h2>");
        out.println("<p>આ પેજ " + counter + " વખત access કરવામાં આવ્યું છે</p>");
        out.println("<p><a href='CounterServlet'>Refresh</a></p>");
        out.println("</body></html>");
        
        out.close();
    }
}

web.xml Configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>CounterServlet</servlet-name>
        <servlet-class>CounterServlet</servlet-class>
        <init-param>
            <param-name>initialCount</param-name>
            <param-value>0</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>CounterServlet</servlet-name>
        <url-pattern>/counter</url-pattern>
    </servlet-mapping>
</web-app>

મુખ્ય લક્ષણો:

  • Thread Safety: synchronized counter increment
  • Initialization: web.xml માંથી counter initialized
  • Persistent: requests ની વચ્ચે counter maintained
  • Configuration: deployment descriptor setup

મેમરી ટ્રીક: “Initialize Synchronize Count Display”


પ્રશ્ન 3(અ) અથવા [3 ગુણ]
#

servlet ના life cycle સમજાવો.

જવાબ:

Servlet Life Cycle આકૃતિ:

stateDiagram-v2
    [*] --> Loading
    Loading --> init()
    init() --> service()
    service() --> service() : Multiple requests
    service() --> destroy()
    destroy() --> [*]

કોષ્ટક: Servlet Life Cycle Methods

MethodહેતુCalled
init()servlet initialize કરે છેstartup પર એક વખત
service()requests handle કરે છેદરેક request માટે
destroy()resources cleanup કરે છેshutdown પર એક વખત

મુખ્ય મુદ્દાઓ:

  • Initialization: servlet load થાય ત્યારે એક વખત call થાય છે
  • Service: બધી client requests handle કરે છે
  • Cleanup: servlet unload થાય તે પહેલાં call થાય છે
  • Container Managed: web container lifecycle ને control કરે છે

મેમરી ટ્રીક: “Initialize Service Destroy”


પ્રશ્ન 3(બ) અથવા [4 ગુણ]
#

Servlet Config class ને યોગ્ય ઉદાહરણ સાથે સમજાવો.

જવાબ:

ServletConfig servlet-specific configuration information અને initialization parameters પ્રદાન કરે છે.

કોષ્ટક: ServletConfig Methods

Methodહેતુ
getInitParameter()init parameter value મેળવે છે
getInitParameterNames()બધા parameter names મેળવે છે
getServletContext()servlet context મેળવે છે
getServletName()servlet name મેળવે છે

ઉદાહરણ:

public class ConfigServlet extends HttpServlet {
    String databaseURL, username;
    
    public void init() throws ServletException {
        ServletConfig config = getServletConfig();
        databaseURL = config.getInitParameter("dbURL");
        username = config.getInitParameter("dbUser");
    }
    
    protected void doGet(HttpServletRequest request, 
                        HttpServletResponse response) 
                        throws ServletException, IOException {
        
        PrintWriter out = response.getWriter();
        out.println("Database URL: " + databaseURL);
        out.println("Username: " + username);
    }
}

web.xml:

<servlet>
    <servlet-name>ConfigServlet</servlet-name>
    <servlet-class>ConfigServlet</servlet-class>
    <init-param>
        <param-name>dbURL</param-name>
        <param-value>jdbc:mysql://localhost:3306/test</param-value>
    </init-param>
    <init-param>
        <param-name>dbUser</param-name>
        <param-value>root</param-value>
    </init-param>
</servlet>

મેમરી ટ્રીક: “Config Gets Parameters Context”


પ્રશ્ન 3(ક) અથવા [7 ગુણ]
#

એક સરળ પ્રોગ્રામ ડેવલપ કરો, જ્યારે વપરાશકર્તા subject code પસંદ કરશે, ત્યારે subject નું નામ servlet અને MySQL database નો ઉપયોગ કરીને પ્રદર્શિત થશે.

જવાબ:

HTML Form (index.html):

<!DOCTYPE html>
<html>
<head>
    <title>વિષય પસંદગી</title>
</head>
<body>
    <h2>વિષય કોડ પસંદ કરો</h2>
    <form action="SubjectServlet" method="get">
        <select name="subjectCode">
            <option value="">વિષય પસંદ કરો</option>
            <option value="4351603">4351603</option>
            <option value="4351604">4351604</option>
            <option value="4351605">4351605</option>
        </select>
        <input type="submit" value="વિષયનું નામ મેળવો">
    </form>
</body>
</html>

Servlet કોડ:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SubjectServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, 
                        HttpServletResponse response) 
                        throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String subjectCode = request.getParameter("subjectCode");
        String subjectName = "";
        
        if(subjectCode != null && !subjectCode.equals("")) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/university", "root", "password");
                
                PreparedStatement ps = con.prepareStatement(
                    "SELECT subject_name FROM subjects WHERE subject_code = ?");
                ps.setString(1, subjectCode);
                
                ResultSet rs = ps.executeQuery();
                if(rs.next()) {
                    subjectName = rs.getString("subject_name");
                }
                
                con.close();
            } catch(Exception e) {
                subjectName = "Error: " + e.getMessage();
            }
        }
        
        out.println("<html><body>");
        out.println("<h2>વિષયની માહિતી</h2>");
        if(!subjectName.equals("")) {
            out.println("<p>વિષય કોડ: " + subjectCode + "</p>");
            out.println("<p>વિષયનું નામ: " + subjectName + "</p>");
        } else {
            out.println("<p>કૃપા કરીને વિષય કોડ પસંદ કરો</p>");
        }
        out.println("<p><a href='index.html'>પાછા જાઓ</a></p>");
        out.println("</body></html>");
    }
}

ડેટાબેઝ ટેબલ:

CREATE TABLE subjects (
    subject_code VARCHAR(10) PRIMARY KEY,
    subject_name VARCHAR(100)
);

INSERT INTO subjects VALUES 
('4351603', 'Advanced Java Programming'),
('4351604', 'Web Technology'),
('4351605', 'Database Management System');

મેમરી ટ્રીક: “Select Query Display Subject”


પ્રશ્ન 4(અ) [3 ગુણ]
#

JSP life cycle સમજાવો.

જવાબ:

JSP Life Cycle આકૃતિ:

stateDiagram-v2
    [*] --> Translation
    Translation --> Compilation
    Compilation --> Loading
    Loading --> jspInit()
    jspInit() --> _jspService()
    _jspService() --> _jspService() : Multiple requests
    _jspService() --> jspDestroy()
    jspDestroy() --> [*]

કોષ્ટક: JSP Life Cycle તબક્કાઓ

તબક્કોવર્ણન
TranslationJSP to Servlet conversion
CompilationServlet to bytecode
Loadingservlet class ને load કરે છે
InitializationjspInit() call થાય છે
Request Processing_jspService() requests handle કરે છે
DestructionjspDestroy() cleanup

મેમરી ટ્રીક: “Translate Compile Load Initialize Service Destroy”


પ્રશ્ન 4(બ) [4 ગુણ]
#

JSP અને Servlet ની સરખામણી કરો.

જવાબ:

કોષ્ટક: JSP vs Servlet સરખામણી

લક્ષણJSPServlet
કોડ પ્રકારHTML with Java codePure Java code
ડેવલપમેન્ટweb designers માટે સરળJava developers માટે વધુ સારું
કમ્પાઈલેશનઆપોઆપમેન્યુઅલ
ફેરફારrestart ની જરૂર નથીrestart જરૂરી
પર્ફોર્મન્સપહેલી request ધીમીઝડપી
જાળવણીસરળજટિલ

મુખ્ય મુદ્દાઓ:

  • ઉપયોગમાં સરળતા: JSP presentation layer માટે સરળ
  • પર્ફોર્મન્સ: Servlet business logic માટે વધુ સારું
  • લવચીકતા: JSP dynamic content માટે વધુ સારું
  • નિયંત્રણ: Servlet વધુ control પ્રદાન કરે છે

મેમરી ટ્રીક: “JSP Easy HTML, Servlet Pure Java”


પ્રશ્ન 4(ક) [7 ગુણ]
#

Enrollment number દ્વારા વર્તમાન સેમેસ્ટરના દરેક વિષયમાં વિદ્યાર્થીની માસિક હાજરી દર્શાવવા માટે JSP web application ડેવલપ કરો.

જવાબ:

Input Form (attendance.html):

<!DOCTYPE html>
<html>
<head>
    <title>વિદ્યાર્થી હાજરી</title>
</head>
<body>
    <h2>વિદ્યાર્થી હાજરી તપાસો</h2>
    <form action="attendanceCheck.jsp" method="post">
        <table>
            <tr>
                <td>Enrollment નંબર:</td>
                <td><input type="text" name="enrollNo" required></td>
            </tr>
            <tr>
                <td>મહિનો:</td>
                <td>
                    <select name="month" required>
                        <option value="">મહિનો પસંદ કરો</option>
                        <option value="January">જાન્યુઆરી</option>
                        <option value="February">ફેબ્રુઆરી</option>
                        <option value="March">માર્ચ</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="હાજરી તપાસો">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

JSP Page (attendanceCheck.jsp):

<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>

<html>
<head>
    <title>હાજરી રિપોર્ટ</title>
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid black; padding: 8px; text-align: center; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h2>માસિક હાજરી રિપોર્ટ</h2>
    
    <%
        String enrollNo = request.getParameter("enrollNo");
        String month = request.getParameter("month");
        
        if(enrollNo != null && month != null) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/university", "root", "password");
                
                // વિદ્યાર્થીની માહિતી મેળવો
                PreparedStatement ps1 = con.prepareStatement(
                    "SELECT name FROM students WHERE enroll_no = ?");
                ps1.setString(1, enrollNo);
                ResultSet rs1 = ps1.executeQuery();
                
                String studentName = "";
                if(rs1.next()) {
                    studentName = rs1.getString("name");
                }
                
                out.println("<p><strong>વિદ્યાર્થી:</strong> " + studentName + 
                           " (" + enrollNo + ")</p>");
                out.println("<p><strong>મહિનો:</strong> " + month + "</p>");
                
                // હાજરી ડેટા મેળવો
                PreparedStatement ps2 = con.prepareStatement(
                    "SELECT s.subject_name, a.total_classes, a.attended_classes, " +
                    "ROUND((a.attended_classes/a.total_classes)*100, 2) as percentage " +
                    "FROM attendance a JOIN subjects s ON a.subject_code = s.subject_code " +
                    "WHERE a.enroll_no = ? AND a.month = ?");
                ps2.setString(1, enrollNo);
                ps2.setString(2, month);
                ResultSet rs2 = ps2.executeQuery();
                
                out.println("<table>");
                out.println("<tr><th>વિષય</th><th>કુલ વર્ગો</th>" +
                           "<th>હાજર થયેલ</th><th>ટકાવારી</th><th>સ્થિતિ</th></tr>");
                
                while(rs2.next()) {
                    String subjectName = rs2.getString("subject_name");
                    int totalClasses = rs2.getInt("total_classes");
                    int attendedClasses = rs2.getInt("attended_classes");
                    double percentage = rs2.getDouble("percentage");
                    String status = percentage >= 75 ? "સારી" : "નબળી";
                    String rowColor = percentage >= 75 ? "lightgreen" : "lightcoral";
                    
                    out.println("<tr style='background-color:" + rowColor + "'>");
                    out.println("<td>" + subjectName + "</td>");
                    out.println("<td>" + totalClasses + "</td>");
                    out.println("<td>" + attendedClasses + "</td>");
                    out.println("<td>" + percentage + "%</td>");
                    out.println("<td>" + status + "</td>");
                    out.println("</tr>");
                }
                
                out.println("</table>");
                con.close();
                
            } catch(Exception e) {
                out.println("<p style='color:red'>Error: " + e.getMessage() + "</p>");
            }
        }
    %>
    
    <br>
    <a href="attendance.html">બીજા વિદ્યાર્થીની તપાસ કરો</a>
</body>
</html>

ડેટાબેઝ ટેબલ્સ:

CREATE TABLE students (
    enroll_no VARCHAR(20) PRIMARY KEY,
    name VARCHAR(50)
);

CREATE TABLE subjects (
    subject_code VARCHAR(10) PRIMARY KEY,
    subject_name VARCHAR(100)
);

CREATE TABLE attendance (
    id INT AUTO_INCREMENT PRIMARY KEY,
    enroll_no VARCHAR(20),
    subject_code VARCHAR(10),
    month VARCHAR(15),
    total_classes INT,
    attended_classes INT,
    FOREIGN KEY (enroll_no) REFERENCES students(enroll_no),
    FOREIGN KEY (subject_code) REFERENCES subjects(subject_code)
);

મેમરી ટ્રીક: “JSP Database Query Display Table”


પ્રશ્ન 4(અ) અથવા [3 ગુણ]
#

JSP માં implicit objects સમજાવો.

જવાબ:

કોષ્ટક: JSP Implicit Objects

ObjectTypeહેતુ
requestHttpServletRequestrequest ડેટા મેળવે છે
responseHttpServletResponseresponse મોકલે છે
outJspWriterclient ને output
sessionHttpSessionsession management
applicationServletContextapplication scope
configServletConfigservlet configuration
pageContextPageContextpage scope access
pageObjectવર્તમાન servlet instance
exceptionThrowableerror page exception

મુખ્ય લક્ષણો:

  • આપોઆપ: declaration વિના ઉપલબ્ધ
  • Scope Access: વિવિધ scope levels
  • Request Handling: input/output operations
  • Session Management: વપરાશકર્તા session tracking

મેમરી ટ્રીક: “Request Response Out Session Application”


પ્રશ્ન 4(બ) અથવા [4 ગુણ]
#

servlet કરતાં JSP શા માટે પસંદ કરવામાં આવે છે તે સમજાવો.

જવાબ:

કોષ્ટક: Servlet કરતાં JSP ના ફાયદા

પાસુંJSP ફાયદો
ડેવલપમેન્ટHTML integration સરળ
જાળવણીpresentation ને logic થી અલગ કરે
કમ્પાઈલેશનઆપોઆપ compilation
ફેરફારserver restart ની જરૂર નથી
ડિઝાઈનweb designer friendly
કોડ પુનઃઉપયોગtag libraries અને custom tags

મુખ્ય મુદ્દાઓ:

  • Separation of Concerns: presentation અને business logic નું સ્પષ્ટ વિભાજન
  • ઝડપી ડેવલપમેન્ટ: ઝડપી development cycle
  • Designer Friendly: web designers HTML-જેવા syntax સાથે કામ કરી શકે
  • આપોઆપ સુવિધાઓ: container compilation અને lifecycle handle કરે

મેમરી ટ્રીક: “Easy HTML Automatic Designer Friendly”


પ્રશ્ન 4(ક) અથવા [7 ગુણ]
#

પાંચ વિષયોના ગુણ સ્વીકારીને વિદ્યાર્થીના ગ્રેડ દર્શાવવા માટે JSP પ્રોગ્રામ વિકસાવો.

જવાબ:

Input Form (gradeInput.html):

<!DOCTYPE html>
<html>
<head>
    <title>વિદ્યાર્થી ગ્રેડ કેલ્ક્યુલેટર</title>
    <style>
        table { margin: auto; border-collapse: collapse; }
        td { padding: 10px; }
        input[type="number"] { width: 100px; }
        input[type="submit"] { padding: 10px 20px; }
    </style>
</head>
<body>
    <h2 style="text-align: center;">વિદ્યાર્થી ગ્રેડ કેલ્ક્યુલેટર</h2>
    <form action="gradeCalculator.jsp" method="post">
        <table border="1">
            <tr>
                <td>વિદ્યાર્થીનું નામ:</td>
                <td><input type="text" name="studentName" required></td>
            </tr>
            <tr>
                <td>વિષય 1 ગુણ:</td>
                <td><input type="number" name="marks1" min="0" max="100" required></td>
            </tr>
            <tr>
                <td>વિષય 2 ગુણ:</td>
                <td><input type="number" name="marks2" min="0" max="100" required></td>
            </tr>
            <tr>
                <td>વિષય 3 ગુણ:</td>
                <td><input type="number" name="marks3" min="0" max="100" required></td>
            </tr>
            <tr>
                <td>વિષય 4 ગુણ:</td>
                <td><input type="number" name="marks4" min="0" max="100" required></td>
            </tr>
            <tr>
                <td>વિષય 5 ગુણ:</td>
                <td><input type="number" name="marks5" min="0" max="100" required></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center;">
                    <input type="submit" value="ગ્રેડ કેલ્ક્યુલેટ કરો">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

JSP Grade Calculator (gradeCalculator.jsp):

<%@ page contentType="text/html;charset=UTF-8" %>

<html>
<head>
    <title>ગ્રેડ પરિણામ</title>
    <style>
        .result-table { 
            margin: auto; 
            border-collapse: collapse; 
            margin-top: 20px;
        }
        .result-table th, .result-table td { 
            border: 1px solid black; 
            padding: 10px; 
            text-align: center; 
        }
        .result-table th { background-color: #f2f2f2; }
        .grade-A { background-color: #90EE90; }
        .grade-B { background-color: #87CEEB; }
        .grade-C { background-color: #F0E68C; }
        .grade-D { background-color: #FFA07A; }
        .grade-F { background-color: #FFB6C1; }
    </style>
</head>
<body>
    <h2 style="text-align: center;">ગ્રેડ રિપોર્ટ</h2>
    
    <%
        String studentName = request.getParameter("studentName");
        
        // ગુણ મેળવો
        int marks1 = Integer.parseInt(request.getParameter("marks1"));
        int marks2 = Integer.parseInt(request.getParameter("marks2"));
        int marks3 = Integer.parseInt(request.getParameter("marks3"));
        int marks4 = Integer.parseInt(request.getParameter("marks4"));
        int marks5 = Integer.parseInt(request.getParameter("marks5"));
        
        // કુલ અને ટકાવારી કેલ્ક્યુલેટ કરો
        int totalMarks = marks1 + marks2 + marks3 + marks4 + marks5;
        double percentage = totalMarks / 5.0;
        
        // ગ્રેડ નક્કી કરો
        String grade;
        String gradeClass;
        if(percentage >= 90) {
            grade = "A+";
            gradeClass = "grade-A";
        } else if(percentage >= 80) {
            grade = "A";
            gradeClass = "grade-A";
        } else if(percentage >= 70) {
            grade = "B";
            gradeClass = "grade-B";
        } else if(percentage >= 60) {
            grade = "C";
            gradeClass = "grade-C";
        } else if(percentage >= 50) {
            grade = "D";
            gradeClass = "grade-D";
        } else {
            grade = "F";
            gradeClass = "grade-F";
        }
        
        // પરિણામ નક્કી કરો
        String result = percentage >= 50 ? "પાસ" : "ફેલ";
    %>
    
    <table class="result-table">
        <tr>
            <th colspan="2">વિદ્યાર્થીની માહિતી</th>
        </tr>
        <tr>
            <td><strong>નામ:</strong></td>
            <td><%= studentName %></td>
        </tr>
        <tr>
            <th colspan="2">વિષય પ્રમાણે ગુણ</th>
        </tr>
        <tr>
            <td>વિષય 1</td>
            <td><%= marks1 %></td>
        </tr>
        <tr>
            <td>વિષય 2</td>
            <td><%= marks2 %></td>
        </tr>
        <tr>
            <td>વિષય 3</td>
            <td><%= marks3 %></td>
        </tr>
        <tr>
            <td>વિષય 4</td>
            <td><%= marks4 %></td>
        </tr>
        <tr>
            <td>વિષય 5</td>
            <td><%= marks5 %></td>
        </tr>
        <tr>
            <th colspan="2">પરિણામ સારાંશ</th>
        </tr>
        <tr>
            <td><strong>કુલ ગુણ:</strong></td>
            <td><%= totalMarks %> / 500</td>
        </tr>
        <tr>
            <td><strong>ટકાવારી:</strong></td>
            <td><%= String.format("%.2f", percentage) %>%</td>
        </tr>
        <tr class="<%= gradeClass %>">
            <td><strong>ગ્રેડ:</strong></td>
            <td><%= grade %></td>
        </tr>
        <tr>
            <td><strong>પરિણામ:</strong></td>
            <td><%= result %></td>
        </tr>
    </table>
    
    <div style="text-align: center; margin-top: 20px;">
        <a href="gradeInput.html">બીજા ગ્રેડની ગણતરી કરો</a>
    </div>
</body>
</html>

ગ્રેડ સ્કેલ કોષ્ટક:

ટકાવારીગ્રેડવર્ણન
90-100A+ઉત્કૃષ્ટ
80-89Aખૂબ સારું
70-79Bસારું
60-69Cસરેરાશ
50-59Dસરેરાશથી નીચે
0-49Fફેલ

મેમરી ટ્રીક: “Calculate Total Percentage Grade Result”


પ્રશ્ન 5(અ) [3 ગુણ]
#

Aspect-oriented programming (AOP) સમજાવો.

જવાબ:

AOP એ programming paradigm છે જે cross-cutting concerns ને business logic થી aspects નો ઉપયોગ કરીને અલગ કરે છે.

કોષ્ટક: AOP મુખ્ય ખ્યાલો

ખ્યાલવર્ણન
Aspectcross-cutting concern ને encapsulate કરતું module
Join Pointprogram execution માં બિંદુ
Pointcutjoin points નો સમૂહ
Advicejoin point પર લેવાતી action
Weavingaspects apply કરવાની પ્રક્રિયા

મુખ્ય લાભો:

  • વિભાજન: business logic ને system services થી અલગ કરે છે
  • મોડ્યુલારિટી: કોડ modularity સુધારે છે
  • પુનઃઉપયોગ: cross-cutting concerns reusable છે
  • જાળવણી: maintain અને modify કરવું સરળ

મેમરી ટ્રીક: “Aspect Join Pointcut Advice Weaving”


પ્રશ્ન 5(બ) [4 ગુણ]
#

Servlet ની વિવિધ વિશેષતાઓની યાદી બનાવો.

જવાબ:

કોષ્ટક: Servlet વિશેષતાઓ

વિશેષતાવર્ણન
Platform IndependentJava સપોર્ટ કરતા કોઈપણ server પર ચાલે છે
Server Independentવિવિધ web servers સાથે કામ કરે છે
Protocol IndependentHTTP, HTTPS, FTP સપોર્ટ કરે છે
Persistentrequests ની વચ્ચે memory માં રહે છે
Robustમજબૂત memory management
SecureBuilt-in security features
Portableએક વખત લખો, ગમે ત્યાં ચલાવો
Powerfulસંપૂર્ણ Java API access

મુખ્ય મુદ્દાઓ:

  • પર્ફોર્મન્સ: CGI કરતાં વધુ સારું પર્ફોર્મન્સ
  • Memory Management: કાર્યક્ષમ memory ઉપયોગ
  • Multithreading: એકસાથે અનેક requests handle કરે છે
  • Extensible: ચોક્કસ protocols માટે extend કરી શકાય છે

મેમરી ટ્રીક: “Platform Server Protocol Persistent Robust”


પ્રશ્ન 5(ક) [7 ગુણ]
#

Model layer, View layer અને Controller layer ને વિગતોમાં સમજાવો.

જવાબ:

MVC આર્કિટેક્ચર આકૃતિ:

graph TB
    U[User] --> C[Controller]
    C --> M[Model]
    M --> C
    C --> V[View]
    V --> U
    
    subgraph "MVC Layers"
        M
        V
        C
    end

કોષ્ટક: MVC Layer વિગતો

LayerજવાબદારીComponentsહેતુ
Modelડેટા અને business logicEntities, DAOs, Servicesડેટા management
ViewPresentation layerJSP, HTML, CSSવપરાશકર્તા interface
ControllerRequest handlingServlets, ActionsFlow control

Model Layer વિગતો:

  • ડેટા Access: ડેટાબેઝ operations અને data persistence
  • Business Logic: મુખ્ય application logic અને rules
  • Validation: ડેટા validation અને integrity checks
  • Entity Classes: ડેટા structures ને represent કરતા Java beans

ઉદાહરણ Model:

public class Student {
    private String enrollNo;
    private String name;
    private double marks;
    
    // Business logic
    public String calculateGrade() {
        if(marks >= 90) return "A";
        else if(marks >= 80) return "B";
        else if(marks >= 70) return "C";
        else return "D";
    }
}

View Layer વિગતો:

  • Presentation: વપરાશકર્તા interface rendering
  • Display Logic: વપરાશકર્તાને ડેટા કેવી રીતે present કરવો
  • User Interaction: forms, buttons, navigation
  • Responsive Design: વિવિધ devices માટે adapt થાય છે

Controller Layer વિગતો:

  • Request Handling: વપરાશકર્તાની requests process કરે છે
  • Flow Control: આગળનું કયું view display કરવું તે નક્કી કરે છે
  • Model Coordination: યોગ્ય model methods ને call કરે છે
  • Response Generation: વપરાશકર્તા માટે response તૈયાર કરે છે

ઉદાહરણ Controller:

@WebServlet("/student")
public class StudentController extends HttpServlet {
    protected void doGet(HttpServletRequest request, 
                        HttpServletResponse response) {
        String action = request.getParameter("action");
        
        if("view".equals(action)) {
            // model માંથી ડેટા મેળવો
            Student student = studentService.getStudent(enrollNo);
            // request scope માં set કરો
            request.setAttribute("student", student);
            // view માં forward કરો
            RequestDispatcher rd = request.getRequestDispatcher("student.jsp");
            rd.forward(request, response);
        }
    }
}

MVC ના ફાયદા:

  • Separation of Concerns: જવાબદારીનું સ્પષ્ટ વિભાજન
  • Maintainability: maintain અને modify કરવું સરળ
  • Testability: દરેક layer ને અલગ થી test કરી શકાય
  • Scalability: મોટા application development ને સપોર્ટ કરે છે
  • Team Development: અનેક developers એકસાથે કામ કરી શકે છે

મેમરી ટ્રીક: “Model Data View Present Controller Handle”


પ્રશ્ન 5(અ) અથવા [3 ગુણ]
#

Spring Boot ની વિશેષતાઓ સમજાવો.

જવાબ:

કોષ્ટક: Spring Boot વિશેષતાઓ

વિશેષતાવર્ણન
Auto Configurationdependencies આધારે આપોઆપ configuration
Starter Dependenciescurated dependencies નો સેટ
Embedded Serversbuilt-in Tomcat, Jetty servers
Production Readyhealth checks, metrics, monitoring
No XML Configurationannotation-based configuration
Developer Toolshot reloading, automatic restart

મુખ્ય લાભો:

  • ઝડપી ડેવલપમેન્ટ: ઝડપી project setup અને development
  • Convention over Configuration: sensible defaults
  • Microservices Ready: સરળ microservices development
  • Cloud Native: cloud deployment માટે તૈયાર

મેમરી ટ્રીક: “Auto Starter Embedded Production Annotation Developer”


પ્રશ્ન 5(બ) અથવા [4 ગુણ]
#

JSP scripting elements પર ટૂંકી નોંધ લખો.

જવાબ:

કોષ્ટક: JSP Scripting Elements

ElementSyntaxહેતુઉદાહરણ
Scriptlet<% %>Java code execution<% int x = 10; %>
Expression<%= %>Output value<%= x + 5 %>
Declaration<%! %>Variable/method declaration<%! int count = 0; %>
Directive<%@ %>Page configuration<%@ page import="java.util.*" %>
Comment<%-- --%>JSP comments<%-- આ comment છે --%>

ઉદાહરણો:

<%-- JSP Comment --%>
<%@ page contentType="text/html" %>

<%! 
    // Declaration - instance variable
    private int counter = 0;
    
    // Declaration - method
    public String getMessage() {
        return "Hello JSP!";
    }
%>

<html>
<body>
    <% 
        // Scriptlet - Java code
        String name = "Student";
        counter++;
    %>
    
    <h1><%= getMessage() %></h1>
    <p>સ્વાગત <%= name %>!</p>
    <p>પેજ <%= counter %> વખત visit કર્યું</p>
</body>
</html>

મુખ્ય મુદ્દાઓ:

  • Scriptlet: Java statements ધરાવે છે
  • Expression: result evaluate કરે અને output આપે છે
  • Declaration: instance variables/methods બનાવે છે
  • Directive: page-level માહિતી પ્રદાન કરે છે

મેમરી ટ્રીક: “Script Express Declare Direct Comment”


પ્રશ્ન 5(ક) અથવા [7 ગુણ]
#

Dependency injection (DI) અને Plain Old Java Object (POJO) ને વિગતોમાં સમજાવો.

જવાબ:

Dependency Injection (DI):

Dependency Injection એ design pattern છે જ્યાં objects તેમની dependencies external source માંથી receive કરે છે internal creation કરવાને બદલે.

કોષ્ટક: DI પ્રકારો

પ્રકારવર્ણનઉદાહરણ
Constructor Injectionconstructor દ્વારા dependenciespublic Service(Repository repo)
Setter Injectionsetter methods દ્વારા dependenciessetRepository(Repository repo)
Field Injectionસીધું field injection@Autowired Repository repo

DI ઉદાહરણ:

// DI વિના - Tight coupling
public class StudentService {
    private StudentRepository repo = new StudentRepository(); // Hard dependency
    
    public Student getStudent(String id) {
        return repo.findById(id);
    }
}

// DI સાથે - Loose coupling
public class StudentService {
    private StudentRepository repo;
    
    // Constructor injection
    public StudentService(StudentRepository repo) {
        this.repo = repo;
    }
    
    public Student getStudent(String id) {
        return repo.findById(id);
    }
}

Spring DI Configuration:

@Service
public class StudentService {
    @Autowired
    private StudentRepository repository;
    
    public List<Student> getAllStudents() {
        return repository.findAll();
    }
}

@Repository
public class StudentRepository {
    public List<Student> findAll() {
        // Database operations
        return studentList;
    }
}

Plain Old Java Object (POJO):

POJO એ સરળ Java object છે જે કોઈ ચોક્કસ framework classes માંથી inherit કરતું નથી અથવા ચોક્કસ interfaces implement કરતું નથી.

POJO લાક્ષણિકતાઓ:

  • કોઈ inheritance નથી: framework classes માંથી extend કરતું નથી
  • કોઈ interfaces નથી: framework interfaces implement કરતું નથી
  • કોઈ annotations નથી: framework annotations વિના કામ કરી શકે છે
  • સરળ: માત્ર business logic અને ડેટા ધરાવે છે

POJO ઉદાહરણ:

// આ એક POJO છે
public class Student {
    private String enrollNo;
    private String name;
    private int age;
    private String course;
    
    // Default constructor
    public Student() {}
    
    // Parameterized constructor
    public Student(String enrollNo, String name, int age, String course) {
        this.enrollNo = enrollNo;
        this.name = name;
        this.age = age;
        this.course = course;
    }
    
    // Getters અને Setters
    public String getEnrollNo() {
        return enrollNo;
    }
    
    public void setEnrollNo(String enrollNo) {
        this.enrollNo = enrollNo;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    // Business methods
    public boolean isEligibleForExam() {
        return age >= 18;
    }
    
    public String getStudentInfo() {
        return "Student: " + name + " (" + enrollNo + "), Course: " + course;
    }
}

DI ના ફાયદા:

  • Loose Coupling: classes વચ્ચે dependencies ઘટાડે છે
  • Testability: testing માટે mock objects inject કરવું સરળ
  • Flexibility: implementations બદલવું સરળ
  • Maintainability: કોડ maintain અને extend કરવું સરળ

POJO ના ફાયદા:

  • સરળતા: સમજવું અને maintain કરવું સરળ
  • Testability: unit test કરવું સરળ
  • Portability: વિવિધ frameworks માં ઉપયોગ કરી શકાય
  • Lightweight: કોઈ framework overhead નથી

DI અને POJO એકસાથે:

// POJO Entity
public class Student {
    private String name;
    private String email;
    // constructors, getters, setters
}

// Service with DI
@Service
public class StudentService {
    @Autowired
    private StudentRepository repository;
    
    public Student createStudent(String name, String email) {
        Student student = new Student(); // POJO creation
        student.setName(name);
        student.setEmail(email);
        return repository.save(student);
    }
}

મેમરી ટ્રીક: “DI Injects Dependencies, POJO Plain Objects”

સંબંધિત

કમ્પ્યુટર નેટવર્કસ અને ડેટા કમ્યુનિકેશન (4361101) - ઉનાળો 2024 સોલ્યુશન
અભ્યાસ-સામગ્રી સોલ્યુશન કમ્પ્યુટર-નેટવર્કસ 4361101 2024 ઉનાળો
એડવાન્સ જાવા પ્રોગ્રામિંગ (4351603) - શિયાળો 2023 સમાધાન
અભ્યાસ-સામગ્રી સમાધાન એડવાન્સ-જાવા 4351603 2023 શિયાળો
ઇલેક્ટ્રોનિક સર્કિટ્સ એન્ડ એપ્લિકેશન્સ (4321103) - ઉનાળુ 2023 સોલ્યુશન
20 મિનિટ
અભ્યાસ-સામગ્રી સોલ્યુશન ઇલેક્ટ્રોનિક-સર્કિટ્સ 4321103 2023 ઉનાળુ
સાયબર સિક્યુરિટી અને ડિજિટલ ફોરેન્સિક્સ (4361601) - ઉનાળુ 2024 સોલ્યુશન
અભ્યાસ-સામગ્રી સોલ્યુશન સાયબર-સિક્યુરિટી 4361601 2024 ઉનાળુ
એન્ટેના એન્ડ વેવ પ્રોપેગેશન (4341106) - વિન્ટર 2024 સોલ્યુશન
22 મિનિટ
Study-Material Solutions Antenna Wave-Propagation 4341106 2024 Winter
ફન્ડામેન્ટલ્સ ઓફ સૉફ્ટવેર ડેવલપમેન્ટ (4331604) - શિયાળું 2023 ઉકેલ
અભ્યાસ-સામગ્રી ઉકેલો સૉફ્ટવેર-ડેવલપમેન્ટ 4331604 2023 શિયાળું