这里我遇到一个问题,通常我必须在Bean类中获取并设置值,因此必须传递给Dbconnection类和servlet,但是我无法在servlet中调用bean / dbconnect方法来插入值在databases.can一些指南,以正确的方式获得jsp传递
豆 - > DBCONNECT --- ---的servlet register.jsp
的welcome.jsp:
<body>
<%
Bean bean=(Bean)request.getAttribute("bean");
out.print("Welcome, "+bean.getUsername());
%>
<p>You are successfully logged in!</p>
</body>
的index.jsp:
<form action="Controller" method="post">
User name:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" name="Login"> <a href="Register.jsp">Register</a>
</form>
Register.jsp
<form action="Controller" method="post">
User name:<input type="text" name="name" >
Password:<input type="password" name="pass">
Email-id<input type="text" name="emailid">
phone number:<input type="text" name="phone">
<input type="submit" value="submit"><a href="index.jsp">Login</a>
</form>
Bean.java
package Databases;
public class Bean
{
private String username;
private String password;
private String emailid;
private String phone;
/**
* @return the username
*/
public String getUsername()
{
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username)
{
this.username = username;
}
/**
* @return the password
*/
public String getPassword()
{
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @return the emailid
*/
public String getEmailid()
{
return emailid;
}
/**
* @param emailid the emailid to set
*/
public void setEmailid(String emailid)
{
this.emailid = emailid;
}
/**
* @return the phone
*/
public String getPhone()
{
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone)
{
this.phone = phone;
}
}
Dbconnect.java
package Databases;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
public class Dbconnect
{
public static void main(String args[]) throws ClassNotFoundException
{
Dbconnect db = new Dbconnect();
db.dbconnect();
}
public Connection dbconnect() throws ClassNotFoundException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String user(Bean bean)
{
Connection con = null;
PreparedStatement prep = null;
try
{
con = dbconnect();
String sql = "SELECT * user WHERE name = ? AND password = ?";
prep = con.prepareStatement(sql);
prep.setString(1, bean.getUsername());
prep.setString(2, bean.getPassword());
}
catch (Exception e)
{
System.out.println("cannot connect");
e.printStackTrace();
}
return "user login";
}
public String insertDetail(Bean bean) throws SQLException
{
String sql = "insert into user(name,password,email,phone) values(?,?,?,?)";
Connection con = null;
PreparedStatement prep = null;
try
{
con = dbconnect();
prep = con.prepareStatement(sql);
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
prep.setString(1, bean.getUsername());
prep.setString(2, bean.getPassword());
prep.setString(3, bean.getEmailid());
prep.setString(4, bean.getPhone());
prep.executeUpdate();
prep.close();
return "successfully added";
}
}
Controller.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.PreparedStatement;
import Databases.Dbconnect;
import Databases.*;
/**
* Servlet implementation class Loginservlet
*/
public class Controller extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
//PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String email = request.getParameter("emailid");
String pn = request.getParameter("phone");
System.out.println("Name :" + name);
System.out.println("pass :" + pass);
System.out.println("emailid :" + email);
System.out.println("phone:" + pn);
Bean bean=new Bean();
bean.setUsername(name);
bean.setPassword(pass);
bean.setEmailid(email);
bean.setPhone(pn);
request.setAttribute("bean",bean);
request.getRequestDispatcher("index.jsp");
request.getRequestDispatcher("Register.jsp");
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WebprojectTry</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Controller</display-name>
<servlet-name>Controller</servlet-name>
<servlet-class>servlet.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
好的,您可以在GitHub account浏览完整的代码。它正在运行,但我个人会将JPA用于持久性。
让我们从包含doPost
方法的控制器开始。它只需要请求参数,构造一些bean并调用DAO将其持久化到数据库。完成后,它将bean设置为请求属性并调用jsp调用。这意味着一旦你再次打开这个jsp,这个bean就不会消失。使用会话将其存储在多个调用中。
public class Controller extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String phone = request.getParameter("phone");
// todo check parameters
User user = new User(name, phone);
DAO dao = new DAO();
dao.saveUser(user);
request.setAttribute("USER", user);// consider using session
// TODO handle errors
RequestDispatcher dispatcher = request.getRequestDispatcher("register/index.jsp");
dispatcher.forward(request, response);
}
}
web.xml很简单:
<servlet>
<servlet-name>RegistrationController</servlet-name>
<servlet-class>cz.literak.sandbox.so.register.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistrationController</servlet-name>
<url-pattern>/RegistrationController</url-pattern>
</servlet-mapping>
DAO使用旧的普通JDBC将对象存储在数据库表中。我赞成使用DataSource。
static {
try {
Class.forName("org.mariadb.jdbc.Driver");
// Class.forName(&#34; com.mysql.jdbc.Driver&#34;); } catch(ClassNotFoundException e){ e.printStackTrace(); } }
public User findUser(String phone) {
// todo
return null;
}
// DDL: CREATE TABLE user (name VARCHAR(100), phone VARCHAR(100))
public boolean saveUser(User user) throws ServletException {
try (
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
PreparedStatement ps = con.prepareStatement("INSERT INTO user VALUES(?,?)"))
{
ps.setString(1, user.getName());
ps.setString(2, user.getPhone());
return ps.execute();
} catch (SQLException e) {
throw new ServletException("DB operation failed", e);
}
}
最后是jsp。它只检查USER是否存在,然后显示注册表单或欢迎消息。
<c:choose>
<c:when test="${USER == null}">
<form action="${pageContext.request.contextPath}/RegistrationController" method="post">
<table>
<tr>
<td>User name</td>
<td><input type="text" name="name" ></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
</c:when>
<c:otherwise>
Welcome <c:out value="${USER.name}"/>
</c:otherwise>
</c:choose>
我希望你不会删除这个问题,因为我花了很多时间来回答。