mercredi 11 mars 2015

How to make a servlet's received attributes available to other servlets via the ServletContext's attribute mechanism?



I have to take in information from servlets using forms and then make the information(Strings) available to other servlets via the ServletContext's attribute method? I do NOT understand this in the slightest and would really appreciate a step-by-step breakdown. Here is my code. One servlet takes an integer and a float and the other a name and surname.


First Servlet



package myServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;


public class FirstServlet extends GenericServlet {

public String firstName;
public String surname;

@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{


PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println("ServletResponse");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>Servlet One</H1>");
out.println("</BODY>");
out.println("</HTML>");
}
}


My Second Servlet



package myServlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;

public class SecondServlet extends GenericServlet {
public String noInteger;
public String noFloat;



String integer = "integer";


@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{

PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println("ServletResponse");
out.println("</TITLE></HEAD>");
out.println("<BODY>");
out.println("<H1>Second Servlet</H1>");
out.println("</BODY>");
out.println("</HTML>");
}
}


My Index.html file



<html>
<head>
<title>Lab One</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="FirstServlet" method="POST">
First Name: <input type="text" name="firstName">
<input type ="submit" name="Submit"> </form>
<br>
<form action="FirstServlet" method="POST">
Surname: <input type="text" name="surname">
<input type ="submit" name="Submit"> </form>
<br>
<form action="SecondServlet" method="POST">
Integer: <input type="text" name="integer">
<input type ="submit" name="Submit2"> </form>
<br>
<form action="SecondServlet" method="POST">
Float: <input type="text" name="float">
<input type ="submit" name="Submit2"> </form>
<br>

</body>
</html>



Aucun commentaire:

Enregistrer un commentaire