Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

unable to pass fields to a servlet from html

teddyjteddyj Member Posts: 37
Thank you in advance. Below are my html, servlet, jsp, and user class, and UserIO class. PLEASE help me why I cant pass data from my html to the servlet. I'm only writing the null values to a text. When I tested the programs the servlet is not getting the data.
--html--
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">






join_email_list.html

Join Wojos Email List

To join our email list, please enter your name and
email address below.

Then, click on the Submit button.

First name:
Last name:
Email address:




-------------------------------------------
The EmailServlet------
package business;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.User;
import data.UserIO;
/**
* @version 1.0
* @author
*/

public class EmailServlet extends HttpServlet{

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
System.out.println("srv fn = " + firstName + " srv ln= " + lastName);


User user = new User(firstName, lastName, emailAddress);
UserIO.addRecord(user,
"C:/murach/webapps/murach/WEB-INF/etc/UserEmail.txt");

RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(
"show_email_entry.jsp");
dispatcher.forward(request, response);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
----the show_email_entry jsp-----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<%@ page
language="java" import="business.*, data.*, java.io.*, java.util.*"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
isThreadSafe="true"
session="true"
autoFlush="true"
%>




show_email_entry


<%@ page import="business.*, data.*, java.util.Date, java.io.*" %>
<%! int accessCount = 0; %>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
System.out.println("fn= " + firstName + " ln= " + lastName);

User user = new User(firstName, lastName, emailAddress);

UserIO.addRecord(user, "C:/murach/webapps/murach/WEB-INF/etc/UserEmail.txt");
int localCount = 0;
synchronized (this) {
accessCount++;
localCount = accessCount;
}

%>

Here is the information that you entered:

First name: <%= firstName %>
Last name: <%= lastName %>
Email address: <%= emailAddress %>

To enter another email address, click on the Back

button in your browser or the Return button shown below.





Todays date is <%= new Date() %>.

This page has been accessed <%= localCount %> times.





-----User.java below---
package business;

/**
* @author tjw
*
* coded really as a JavaBean
*/
public class User {
private String firstName;
private String lastName;
private String emailAddress;

public User(){}

public User(String first, String last, String email){
firstName = first;
lastName = last;
emailAddress = email;
}

public void setFirstName(String f){
firstName = f;

}

public String getFirstName(){ return firstName; }

public void setLastName(String l){
lastName = l;
}
public String getLastName(){ return lastName; }

public void setEmailAddress(String e){
emailAddress = e;
}
public String getEmailAddress(){ return emailAddress;

}


}
-----the UserIO.java below-----
package data;

import business.User;
import java.io.*;
/**
* @author wojo
* PrintWriter writes strings & numbers to a character string
* Filewriter converts characters to bytes and sends them to a file
* together they are a filtered stream
* "../webapps/murach/WEB-INF/etc/UserEmail.txt" is in filename
*
*/
public class UserIO {
public synchronized static void addRecord(User user, String filename)
throws IOException{
System.out.println("filename = " + filename);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(filename, true)));

out.println(user.getEmailAddress() + "|"
+ user.getFirstName() +"|"
+ user.getLastName());
out.close();
}
}

----the servlet---

Comments

  • treshrtreshr Member Posts: 326
    I looked at you html page and think I've found a problem.
    You don't have a tag in your html page, so if you click the send button it just doesn't go anywhere.
    Insert a tag with as method "post" and action the servlet, I'm sure that when you send the form after you inserted the code, you'll get values into the servlet.
    Hope this helps you,

    --=][tReShR][=--

  • teddyjteddyj Member Posts: 37
    : I looked at you html page and think I've found a problem.
    : You don't have a tag in your html page, so if you click the send button it just doesn't go anywhere.
    : Insert a tag with as method "post" and action the servlet, I'm sure that when you send the form after you inserted the code, you'll get values into the servlet.
    : Hope this helps you,
    :
    : --=][tReShR][=--
    :
    :



  • teddyjteddyj Member Posts: 37
    : : I looked at you html page and think I've found a problem.
    : : You don't have a tag in your html page, so if you click the send button it just doesn't go anywhere.
    : : Insert a tag with as method "post" and action the servlet, I'm sure that when you send the form after you inserted the code, you'll get values into the servlet.
    : : Hope this helps you,
    : :
    : : --=][tReShR][=--
    : :
    : : Thank You. That was it.
    :
    :
    :



Sign In or Register to comment.