Two Laptop Bag*
The moose likes Servlets and the fly likes in which method shuld i use at the servlet code for request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "in which method shuld i use at the servlet code for request " Watch "in which method shuld i use at the servlet code for request " New topic
Author

in which method shuld i use at the servlet code for request

yoop
Greenhorn

Joined: May 05, 2001
Posts: 3
in which method shuld i use at the servlet code for request
the value of a radio button in the html code?

<html>
<head></head>
<body>
<center><font color=darkred><h1>REGISTRATION FORM</h1></font></center>
<form method=post action="http://localhost:8080/examples/servlet/DataWare">
<font size=4 color=darkblue><b>
first name : <input type=text size=20 name="fn">
<br><br>
last name : <input type=text size=20 name="ln">
<br><br>
Id number : <input type=text size=20 name="id">
<br><br>
User name : <input type=text size=20 name="un">
<br><br>
password : <input type=password size=20 name="passw">
<br><br>
age : <input type=text size=3 name="age">
<br><br>
address : <input type=text size=20 name="add">
<br><br>
type of registration :
<br>
<!------------------------------------------------------------------------------------------------------------------------------->
<input type=radio name="teachorstu" value="teacher">teacher
<br><br> <!-^^^^^^^^^^^^^^->
<input type=radio name="teachorstu" value="student">student
<!--------------here is the problem ^^^^^^^^^^^^^-------------->
<br><br>
</b></font>
<input type=submit value="register">
<br><br>
</form>
</body>
</html>
and the java source code::
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataWare extends HttpServlet
{
private String firstName;
private String lastName;
private String id;
private String userName;
private String password;
private String age;
private String address;
private String type;
private static Connection connection;
public void init()
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch(Exception e)
{
System.out.println("Registering the Driver was failed");
}
try
{
connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/ExamDB");
}
catch(Exception exception)
{
System.out.println("Connection with the DataBase failed");
exception.printStackTrace();
System.exit(1);
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
Students student =new Students(connection);
res.setContentType("text/html");
firstName=req.getParameter("fn");
lastName=req.getParameter("ln");
id=req.getParameter("id");
userName=req.getParameter("un");
password=req.getParameter("passw");
age=req.getParameter("age");
address=req.getParameter("add");
/***************************************************************/
type=req.getParameterValues("teachorstu");
/**the problem is here^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^******/
try
{
student.addNewStudent(firstName,lastName,id,userName,password,age,address,type);
}
catch(SQLException sql){}
try
{
if(type=="teacher")
{
PrintWriter out=res.getWriter();
out.println("<html><body width=400 height=400><h1>TEACHER ADMINISTRATION PAGE FOR : "+firstName.toUpperCase()+"</h1>");
out.println("<br><br><input type=submit value='create exam' action=''>");
out.println("<br><br><br><input type=submit value='exam results' action=''>");
out.println("</body></html>");
out.close();
}
else
{
PrintWriter out=res.getWriter();
out.println("<html><body width=400 height=400><h1>STUDENT ADMINISTRATION PAGE FOR : "+firstName.toUpperCase()+"</h1>");
out.println("<br><br><input type=submit value='do exam' action=''>");
out.println("<br><br><br><input type=submit value='exam results' action=''>");
out.println("</body></html>");
out.close();
}
}
catch(Exception e){}
}
}

class Students
{
private Connection connect;
private PreparedStatement addNewStudento;
public Students(Connection c)
{
connect = c;
try
{
addNewStudento = connect.prepareStatement
("INSERT INTO students VALUES (?,?,?,?,?,?,?,?)");
}
catch (SQLException exception)
{
exception.printStackTrace();
}
}
public void addNewStudent(String firstn, String lastn, String idn,
String usern, String passn, String agen, String add, String typen)
throws SQLException
{
addNewStudento.setString(1,firstn);
addNewStudento.setString(2,lastn);
addNewStudento.setString(3,idn);
addNewStudento.setString(4,usern);
addNewStudento.setString(5,passn);
addNewStudento.setString(6,agen);
addNewStudento.setString(7,add);
addNewStudento.setString(8,typen);
if (addNewStudento.executeUpdate()!=1)
{
throw new SQLException("adding new Student failed");
}
}
}
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Since you are using radio buttons you can change how you retrieve the valu from "type=req.getParameterValues("teachorstu")" to "type=req.getParameter("teachorstu"). i.e only one value can be sent to the servlet. Also you may want to change if(type=="teacher") to if(type.equals("teacher")) Put your code in the doPost method

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
You are headed for trouble with this code:

Remember that servlets are multithreaded. Using instance variables like this allows multiple requests to interfere with each other. Move these variable declarations inside your doGet() or doPost() method.
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
Website: http://www.philhanna.com


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"yoop",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: in which method shuld i use at the servlet code for request