• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}
}
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!