1

how do i set the jsp file as response in my servlet ?And what kind of expression do i need to use in the jsp page? this is the form

<html>
<head>
    <title>Select your Hobby</title>
</head>
<body>
<form method="POST" action="SelectHobby">
    <p> Choose a Hobby:
    </p>
    <select name="hobby" size="1">
        <option>horse skiing
        <option>extreme knitting
        <option>alpine scuba
        <option>speed dating
    </select>
    <br><br>
    <center>
        <input type="SUBMIT">
    </center>
</form>
</body>
</html>

the servlet

package com.example.web;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HobbyServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        String hobby = request.getParameter("hobby");
    }
}

and this is the jsp in wich i want to see the hobby in the body

<html>
<head>
    <title>These are your hobbies</title>
</head>
<body>
<%
</body>
</html>
helloApp
  • 449
  • 1
  • 4
  • 21

1 Answers1

2

I recomend you to read this post. JSTL will help you. I suppose you mapped your servlet. Here is a quick example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
       <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="testservlet">
    <p> Choose a Hobby:
    </p>
    <select name="hobby" size="1">
        <option>horse skiing
        <option>extreme knitting
        <option>alpine scuba
        <option>speed dating
    </select>
    <br><br>
    <center>
        <input type="SUBMIT">
    </center>
</form>
</body>
</html>

The servlet:

@WebServlet(name = "test", urlPatterns = { "/testservlet" }) 
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;

public Test() {
    super();
}   

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    String hobby = request.getParameter("hobby");   
    request.setAttribute("theHobby", hobby);
    request.getRequestDispatcher("hobbypage.jsp").forward(request, response);
}

I defined an attribute called theHobby to hold the value from the selected hobby in the first page. I also created a page called hobbypage.jsp where I want to send the value to.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
       <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hobby: ${theHobby}
</body>
</html>

With JSTL you can call the attribute through the name you defined like this ${nameOfAttribute}.

rhenesys
  • 174
  • 1
  • 11