java scope

 avatar
user_3924995
plain_text
5 months ago
3.3 kB
2
Indexable
index jsp
**************************************************************************
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>First Page</title>
</head>
<body>
<%
    ArrayList<String> listOfAnimals = new ArrayList<>();
    listOfAnimals.add("dog");
    listOfAnimals.add("horse");
    listOfAnimals.add("rabbit");

    getServletContext().setAttribute("animals", listOfAnimals);
            ArrayList<String> availableAnimals = (ArrayList<String>)getServletContext().getAttribute("animals");
  %>


    <form action="second.jsp" method="post">
    Name: <input type="text" name="name"> </br>
    Select yout favourite animal: </br>
    <select name="animal">
        <% for (String a : availableAnimals){ %>
    <option><%=a%></option>
        <% } %>
        </select> <br/>
        <input type="submit" value="Select animal">
    </form>

</body>
</html>
**************************************************************************


second jsp
**************************************************************************

<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: hallgato
  Date: 11/26/2024
  Time: 2:22 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Second page</title>
</head>
<body>
<%
if(request.getParameter("name") != null) {
    session.setAttribute("username", request.getParameter("name"));
    session.setAttribute("favoriteAnimal", request.getParameter("animal"));
}
if(request.getParameter("updateAnimal") != null){
    session.setAttribute("favoriteAnimal", request.getParameter("updateAnimal"));
}
String username = (String)session.getAttribute("username");
String animal = (String)session.getAttribute("favoriteAnimal");
    ArrayList<String> availableAnimals = (ArrayList<String>) getServletContext().getAttribute("animals");
%>

<h2>Welcome, <%=username%> to my website</h2>
<h3>Your favorite animal is: <%=animal%></h3>
<form action="second.jsp" method="post">
    Change your favourite animal: </br>
    <select name="updateAnimal">
        <% for (String a : availableAnimals){ %>
        <option><%=a%></option>
        <% } %>
    </select> <br/>
    <input type="submit" value="Select animal">

</form>
</body>
</html>




hyperlink is like a get method(doGet in servlet)

**************************************************************************
servlet
**************************************************************************
package com.example.practice;

import java.io.*;

import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + message + "</h1>");
        out.println("</body></html>");
    }

    public void destroy() {
    }
}

Editor is loading...
Leave a Comment