我正在准备一个表单,其中所有数据都将由用户提供,并将插入到mysql中。
在这种形式中,我必须使用长文本,我使用文本区域,但我无法获得教程如何在html表下使用textarea,并且单击提交按钮同样应保存到mysql表。
任何人都可以指导我吗?
答案 0 :(得分:-1)
这是Html文件 MySignUp.html
<html>
<head>
<title>My Sign Up Page</title>
</head>
<body>
<form action="login" method=post"> //from here control goes to web.xml
<table>
<tr> <td>Enter your Name: </td>
<td><input type="textbox" name="name" ></input></td>
</tr>
<tr> <td>Enter your phone Number: </td>
<td><input type="textbox" name="phone" ></input></td>
</tr>
<tr >
<td colspan="2"><textarea name="desc" ></textrea></td>
</tr>
<tr> <td><input type="submit" value="Sign Up"/></td>
<td><input type="reset" value="Reset"/></td>
</tr>
</table>
</form>
</body>
</html>
这是从用户单击“注册”按钮然后控件转到web.xml
时的基本注册web.xml文件是一个将html请求映射到servlet的映射文件。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Sign Up Application</display-name>
<servlet>
<servlet-name>SignServlet</servlet-name>
<servlet-class>com.controller.Sign</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SignServlet</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>
</web-app>
这是注册Servlet SignServlet.java
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SignServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
String name= request.getParameter("name");
String phone=request.getparameter("phone");
String desc=request.getparameter("desc");
if(name != "" && phone != "" && desc != ""){
SignDao signDao=new SignDao();
if(signDao.add(name , phone , desc){
//redirect to success page
}
else{
//redirect to failure page
}
}else{
//redirect to failure page
}
}
这是数据库文件 SignDao.java
package com.dao;
import java.sql.*;
public class SignDao {
public boolean add(String name , String phone , String desc){
try{
Connection con=ConnectionProvider.getConnection();
PreparedStatement ps=con.prepareStatement("insert into users values( name=? , phone=? , desc=?)");
ps.setString(1,name);
ps.setString(2, phone);
ps.setString(3, desc);
if(ps.executeUpdate()>0)
return true;
else
return false;
}catch(Exception e){}
return false;
}
}
这是连接提供程序 ConnectionProvider.java
package com.util;
import java.sql.*;
import static com.constants.Provider.*;
public class ConnectionProvider {
private static Connection con=null;
static{
try{
Class.forName(DRIVER);
con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);
}catch(Exception e){}
}
public static Connection getConnection(){
return con;
}
}
这是数据库常量 Provider.java
package com.constants;
public interface Provider {
String DRIVER="com.mysql.jdbc.Driver";
String CONNECTION_URL="jdbc:mysql://localhost:3030/myDB";
String USERNAME="system";
String PASSWORD="oracle";
}