我是servlet和jsp的新手所以我正在尝试实现视图,添加,更新和删除等操作,但我没有想法实现此操作,如如何转发页面并维护会话并将值传递给jsp如此善良,有些人可能会以正确的方式提供帮助。 我已经去了很多教程,但不知道,这里尝试了
Controller.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import db.Dbconnect;
public class Controller extends HttpServlet
{
private static final long serialVersionUID = 1L;
public Controller()
{
super();
}
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 nam = request.getParameter("name");
String pass = request.getParameter("pass");
String email = request.getParameter("emailid");
String phono = request.getParameter("phone");
}
public void view() throws ClassNotFoundException
{
try
{
String sql="select * from Reg";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
rs.getString("name");
rs.getString("pass");
rs.getString("emailid");
rs.getString("phoneno");
System.out.println(rs.getString("name"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return;
}
public String insertuser() throws SQLException
{
String sql = "insert into Reg(name,pass,emailid,phoneno) values(?,?,?,?)";
Connection con = null;
PreparedStatement prep = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
prep = con.prepareStatement(sql);
prep.setString(1, "nam");
prep.setString(2, "pass");
prep.setString(3, "email");
prep.setString(4, "phono");
prep.executeUpdate();
prep.close();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "successfully added";
}
public String update() throws SQLException
{
String sql = "update Reg set pass=?,emailid=?,phoneno=? where name=?";
Connection con = null;
PreparedStatement prep = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
prep = con.prepareStatement(sql);
prep.setString(1, "nam");
prep.setString(2, "pass");
prep.setString(3, "email");
prep.setString(4, "phono");
prep.executeUpdate();
prep.close();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "successfully update";
}
public String delete() throws SQLException
{
String sql = "delete form Reg where name=?";
Connection con = null;
PreparedStatement prep = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
prep = con.prepareStatement(sql);
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "successfully delete";
}
}
Add.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">
</form>
Update.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="Update"/>
</form>
的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>Vweb2</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>
这个两页的 View.jsp
和delete.jsp
我不知道如何将信息servlet检索到jsp
答案 0 :(得分:2)
我认为你会得到更好的帮助,例如。 http://danielniko.wordpress.com/2012/04/17/simple-crud-using-jsp-servlet-and-mysql/
答案 1 :(得分:0)
对于View.jsp,将表单的方法属性声明为GET。
在下面,
<form action="/Controller" method="get">
并使用
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// Your rendering logic
}
类似地,对delete.jsp使用delete / doDelete()方法。
您还需要在web.xml中提及映射