本文介绍了JSP和SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have created a JSP page(index.jsp),success page(success.html),error page(error.html) and a servlet page(Login.java).
Now i want that whatever information the user enters in the login for(that is index.jsp)..that information should be stored in the SQL.
I have tried the following code:-
[CODE]
index.jsp:-
Collapse | Copy Code
<%-- Document : index Created on : Aug 28, 2010, 8:04:10 AM Author : Prateek--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Login Form </title>
</head>
<body
<form action="success.html" method="get"/>
<br>
<br>
<center><h3> Please fill in the following information in order to continue with your registration</h3></center>
<br>
<br>
<table border="2" bordercolor="black" align="center" cellpadding="5" cellspacing="2" width="450" height="300">
<tr>
<td ><center><font color="black"><b>First Name </b></font></center></td>
<td><center><input type="text" name="name"></center></td>
</tr>
<tr>
<td><center><font color="black"><b>Last Name</b></font></center></td>
<td><center><input type="text" name="name"></center></td>
</tr>
<tr>
<td><center><font color="black"><b>Address</b></font></center></td>
<td><center><input type="text" name="address"></center></td>
</tr>
<tr>
<td><center><font color="black"><b>Contact Number</b></font></center></td>
<td><center><input type="text" name="cno"></center></td>
</tr>
<tr>
<td><center><font color="black"><b>E-Mail ID</b></font></center></td>
<td><center><input type="text" name="E-Mail"></center></td>
</tr>
<tr>
<td><center><font color="black"><b>Registration Date</b></font></center></td>
<td><center><input type="text" name="Rd"/></center></td>
</tr>
</table>
</body>
<br>
<br>
<center><input type="submit" value=" Save "> <input type="reset" value=" Reset " onclick="location.reload()"></center>
</html>
Login.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;
public class Login extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String fn =request.getParameter("First Name");
String ln =request.getParameter("Last Name");
String address=request.getParameter("Address");
String cno=request.getParameter("cno");
String EMail=request.getParameter("E-Maild ID");
String Rd=request.getParameter("Registration Date");
try
{
if(fn.length()==0 && ln.length()==0 && address.length()==0 && cno.length()==0 && EMail.length()==0 && Rd.length()==0)
{
response.sendRedirect("error.html");
}
else
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:Family","","");
PreparedStatement ps = c.prepareStatement("insert into Friends (FirstName,LastName,Address,ContactNo,EMailID,RegistrationDate) values(?,?,?,?,?,?)");
ps.setString(1, fn);
ps.setString(2, ln);
ps.setString(3, address);
ps.setString(4, cno);
ps.setString(5, EMail);
ps.setString(6, Rd);
ps.executeUpdate();
response.sendRedirect("success.html");
}
}
catch(Exception ec)
{
out.print(ec);
}
finally
{
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
@Override
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
[/CODE]
now i have provided with the coding of my projct. So plz help me...in creating the coonection to the SQL.
推荐答案
<form action="success.html" method="get" />
也许应该是:
maybe should be:
<form action="/Login" method="get">
... your inputs here
</from>
</form>
也不是参数名称,这是* label *
also isn''t a parameter name this is *label*
String fn =request.getParameter("First Name");
在您的html中看到
see in your html
------------------------------------[HERE]
<td><center><font color="black"><b>First Name </b></font></center></td>
<td><center><input type="text" name="name"></center>
</input>
而不是尝试运行页面并查看错误,也许没有错误,没有人可以给您任何解决方案.
than try to run your page and see errors, maybe without errors no one can give you any solution.
这篇关于JSP和SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!