servlet端调试出错.error message:
Ctx(/WebApp) : Status code:405 request:R( /WebApp + /employeeservlet + null) msg:HTTP method GET is not supported by this URL/*servlet端*/
package com.zcsoft.EmployeeServlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import serializable.EmployeeData;
import java.sql.ResultSet;public class EmployeeServlet extends HttpServlet
{
//Initialize global variables
public void init() throws ServletException
{
}
//Process the HTTP Get request
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
DBManager dbBean=new DBManager();
EmployeeData employee=new EmployeeData();
ObjectInputStream in=new ObjectInputStream(request.getInputStream());
try
{
employee=(EmployeeData)in.readObject();
if(employee.getAction()=="Select")
{
java.sql.ResultSet rs;
java.util.Vector data=null;
rs=dbBean.excuteQuery("select * from basic");
try
{
while(rs.next())
{
data.add(rs.getString(1));
data.add(rs.getString(2));
data.add(rs.getString(3));
data.add(rs.getString(4));
data.add(rs.getString(5));
data.add(rs.getString(6));
data.add(rs.getString(7));
data.add(rs.getString(8));
}
}
catch(java.sql.SQLException exe)
{
System.out.println("sql exception!");
in.close();
}
finally
{
in.close();
}
}
if(employee.getAction()=="Update")
{
employee.setRowChanged(dbBean.excuteUpdate("update basic set name='"+employee.getName()+"',birth='"+employee.getBirth()+"',gender="+employee.getBirth()+",edu="+employee.getEdu()+"tel='"+employee.getTel()+"',email='"+employee.getEmail()+"',address='"+employee.getAddress()+"' "+"where num="+employee.getNum()));
in.close();
}
if(employee.getAction()=="Delete")
{
employee.setRowChanged(dbBean.excuteUpdate("delete from basic where num="+employee.getNum()));
in.close();
}
if(employee.getAction()=="Add")
{
employee.setRowChanged(dbBean.excuteUpdate("insert into basic(name,birth,gender,edu,tel,email,address) values('"+employee.getName()+"','"+employee.getBirth()+"',"+employee.getGender()+","+employee.getEdu()+",'"+employee.getTel()+"','"+employee.getEmail()+"','"+employee.getAddress()+"'"));
in.close();
}
ObjectOutputStream out=new ObjectOutputStream(response.getOutputStream());
out.writeObject(employee);
response.setContentType("application/octest-stream");
out.flush();
out.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(java.lang.ClassNotFoundException exe)
{
exe.printStackTrace();
}
finally
{
//dbBean.dispose();
}
}
//Clean up resources
public void destroy()
{
super.destroy();
}
}package serializable;public class EmployeeData implements java.io.Serializable
{
/**/
private String name=null;
private String birth=null;
private boolean gender;
private int edu;
private String tel=null;
private String email=null;
private String address=null;
private String action=null;
private int num;
private java.util.Vector data=null;
private boolean rowChange;
public int getNum()
{
return this.num;
}
public void setNum(int num)
{
this.num=num;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
public String getBirth()
{
return this.birth;
}
public void setBirth(String birth)
{
this.birth=birth;
}
public String getTel()
{
return this.tel;
}
public void setTel(String tel)
{
this.tel=tel;
}
public String getEmail()
{
return this.email;
}
public void setEmail(String email)
{
this.email=email;
}
public String getAddress()
{
return this.address;
}
public void setAddress(String address)
{
this.address=address;
}
public boolean getGender()
{
return this.gender;
}
public void setGender(boolean gender)
{
this.gender=gender;
}
public int getEdu()
{
return this.edu;
}
public void setEdu(int edu)
{
this.edu=edu;
}
public String getAction()
{
return this.action;
}
public void setAction(String action)
{
this.action=action;
}
public boolean getRowChanged()
{
return this.rowChange;
}
public void setRowChanged(boolean rowChange)
{
this.rowChange=rowChange;
}
public java.util.Vector getData()
{
return this.data;
}
public void setData(java.util.Vector data)
{
this.data=data;
}
}package com.zcsoft.EmployeeServlet;import java.sql.*;
import java.io.*;public class DBManager
{
public java.sql.ResultSet rs=null;
public java.sql.Statement stmt=null;
public java.sql.Connection con=null;
public java.sql.PreparedStatement presta=null;
public java.sql.CallableStatement casta=null;
public String username="sa";
public String password="test";
public String driver="sun.jdbc.odbc.JdbcOdbcDriver";
public String url="Jdbc:Odbc:Employee";
public DBManager()
{
try
{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
}
catch(Exception e)
{
System.err.println("驱动加载错误或用户名密码出错"+e.toString());
}
}
public ResultSet excuteQuery(String sql)
{
try
{
stmt=con.createStatement();
return rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
stmt=null;
}
}
public boolean excuteUpdate(String sql)
{
try
{
stmt=con.createStatement();
return stmt.execute(sql);
}
catch(SQLException ex)
{
ex.printStackTrace();
return false;
}
finally
{
stmt=null;
}
}
public void dispose()
{
if(con!=null)
{
if(stmt!=null)
{
if(rs!=null)
{
try
{
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
rs=null;
}
}
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
stmt=null;
}
} }
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
con=null;
}
}
}
Ctx(/WebApp) : Status code:405 request:R( /WebApp + /employeeservlet + null) msg:HTTP method GET is not supported by this URL/*servlet端*/
package com.zcsoft.EmployeeServlet;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import serializable.EmployeeData;
import java.sql.ResultSet;public class EmployeeServlet extends HttpServlet
{
//Initialize global variables
public void init() throws ServletException
{
}
//Process the HTTP Get request
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
DBManager dbBean=new DBManager();
EmployeeData employee=new EmployeeData();
ObjectInputStream in=new ObjectInputStream(request.getInputStream());
try
{
employee=(EmployeeData)in.readObject();
if(employee.getAction()=="Select")
{
java.sql.ResultSet rs;
java.util.Vector data=null;
rs=dbBean.excuteQuery("select * from basic");
try
{
while(rs.next())
{
data.add(rs.getString(1));
data.add(rs.getString(2));
data.add(rs.getString(3));
data.add(rs.getString(4));
data.add(rs.getString(5));
data.add(rs.getString(6));
data.add(rs.getString(7));
data.add(rs.getString(8));
}
}
catch(java.sql.SQLException exe)
{
System.out.println("sql exception!");
in.close();
}
finally
{
in.close();
}
}
if(employee.getAction()=="Update")
{
employee.setRowChanged(dbBean.excuteUpdate("update basic set name='"+employee.getName()+"',birth='"+employee.getBirth()+"',gender="+employee.getBirth()+",edu="+employee.getEdu()+"tel='"+employee.getTel()+"',email='"+employee.getEmail()+"',address='"+employee.getAddress()+"' "+"where num="+employee.getNum()));
in.close();
}
if(employee.getAction()=="Delete")
{
employee.setRowChanged(dbBean.excuteUpdate("delete from basic where num="+employee.getNum()));
in.close();
}
if(employee.getAction()=="Add")
{
employee.setRowChanged(dbBean.excuteUpdate("insert into basic(name,birth,gender,edu,tel,email,address) values('"+employee.getName()+"','"+employee.getBirth()+"',"+employee.getGender()+","+employee.getEdu()+",'"+employee.getTel()+"','"+employee.getEmail()+"','"+employee.getAddress()+"'"));
in.close();
}
ObjectOutputStream out=new ObjectOutputStream(response.getOutputStream());
out.writeObject(employee);
response.setContentType("application/octest-stream");
out.flush();
out.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(java.lang.ClassNotFoundException exe)
{
exe.printStackTrace();
}
finally
{
//dbBean.dispose();
}
}
//Clean up resources
public void destroy()
{
super.destroy();
}
}package serializable;public class EmployeeData implements java.io.Serializable
{
/**/
private String name=null;
private String birth=null;
private boolean gender;
private int edu;
private String tel=null;
private String email=null;
private String address=null;
private String action=null;
private int num;
private java.util.Vector data=null;
private boolean rowChange;
public int getNum()
{
return this.num;
}
public void setNum(int num)
{
this.num=num;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
public String getBirth()
{
return this.birth;
}
public void setBirth(String birth)
{
this.birth=birth;
}
public String getTel()
{
return this.tel;
}
public void setTel(String tel)
{
this.tel=tel;
}
public String getEmail()
{
return this.email;
}
public void setEmail(String email)
{
this.email=email;
}
public String getAddress()
{
return this.address;
}
public void setAddress(String address)
{
this.address=address;
}
public boolean getGender()
{
return this.gender;
}
public void setGender(boolean gender)
{
this.gender=gender;
}
public int getEdu()
{
return this.edu;
}
public void setEdu(int edu)
{
this.edu=edu;
}
public String getAction()
{
return this.action;
}
public void setAction(String action)
{
this.action=action;
}
public boolean getRowChanged()
{
return this.rowChange;
}
public void setRowChanged(boolean rowChange)
{
this.rowChange=rowChange;
}
public java.util.Vector getData()
{
return this.data;
}
public void setData(java.util.Vector data)
{
this.data=data;
}
}package com.zcsoft.EmployeeServlet;import java.sql.*;
import java.io.*;public class DBManager
{
public java.sql.ResultSet rs=null;
public java.sql.Statement stmt=null;
public java.sql.Connection con=null;
public java.sql.PreparedStatement presta=null;
public java.sql.CallableStatement casta=null;
public String username="sa";
public String password="test";
public String driver="sun.jdbc.odbc.JdbcOdbcDriver";
public String url="Jdbc:Odbc:Employee";
public DBManager()
{
try
{
Class.forName(driver);
con=DriverManager.getConnection(url,username,password);
}
catch(Exception e)
{
System.err.println("驱动加载错误或用户名密码出错"+e.toString());
}
}
public ResultSet excuteQuery(String sql)
{
try
{
stmt=con.createStatement();
return rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
finally
{
stmt=null;
}
}
public boolean excuteUpdate(String sql)
{
try
{
stmt=con.createStatement();
return stmt.execute(sql);
}
catch(SQLException ex)
{
ex.printStackTrace();
return false;
}
finally
{
stmt=null;
}
}
public void dispose()
{
if(con!=null)
{
if(stmt!=null)
{
if(rs!=null)
{
try
{
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
rs=null;
}
}
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
stmt=null;
}
} }
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
con=null;
}
}
}
解决方案 »
- struts2搭建,新手,Struts-2.3.7-all.zip需要哪些jar???
- 使用Mybatis Generator自动生成Mybatis相关代码,修改生成xml格式
- 数据库主键设定
- forward基础问题
- 问一个关于struts很菜的问题
- request.setCharacterEncoding("gb2312")怎么用
- 这个hql如何写
- 谁能简单介绍一下Vector类,和TreeMap类
- bird_flying是个jsp新手,希望找个有经验的人学习一下,请发信到[email protected],多谢!!
- java 初学者求助有关struct配置的问题 "Invalid path /startExam was requested"
- [求助]如何利用文本框的onChange事件实现到后台的用户ID检测?
- 一个关于spawn的问题
throws IOException, ServletException
看看。
service方法是用来判断客户请求方式的(GET/POST),他会根据请求方式调用doGet/doPost
给brower端.写成多层结构,暂时有点难度.to:Kelvin316,brower是有一处出现URL不正确.brower端代码如下:
public void actionPerformed(ActionEvent e)
{
EmployeeData employee=new EmployeeData();
java.net.URLConnection con=null;
try
{
if(e.getActionCommand().equals("Update"))
{
URL url=new URL("http://localhost:8083/WebApp/server");
con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
fillEmployee();
employee.setAction("Update");
ObjectOutputStream oos=new ObjectOutputStream(con.getOutputStream());
oos.writeObject(employee);
oos.flush();
oos.close();
ObjectInputStream ois=new ObjectInputStream(con.getInputStream());
try
{
employee=(EmployeeData)ois.readObject();
ois.close();
if(employee.getRowChanged())
JOptionPane.showConfirmDialog(null,"Update Successfully!","Update Successfully",JOptionPane.OK_OPTION);
JOptionPane.showConfirmDialog(null,"Update Failed!","Update Failed!",JOptionPane.OK_OPTION);
}
catch(java.lang.ClassNotFoundException ex)
{
System.out.println("class not found!");
}
catch(java.io.IOException exe)
{
System.out.println("IO Exception");
}
finally
{
con=null;
}
jtbEmployee.setModel(new TableModel());
jtbEmployee.updateUI();
}
if(e.getActionCommand().equals("Delete"))
{
URL url=new URL("http://localhost:8083/WebApp/server");
con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
ObjectOutputStream oos=new ObjectOutputStream(con.getOutputStream());
employee.setNum(num);
employee.setAction("Delete");
oos.writeObject(employee);
oos.flush();
oos.close();
ObjectInputStream ois=new ObjectInputStream(con.getInputStream());
try
{
employee=(EmployeeData)ois.readObject();
ois.close();
if(employee.getRowChanged())
JOptionPane.showConfirmDialog(null,"Delete Successfully!","Delete Successfully",JOptionPane.OK_OPTION);
JOptionPane.showConfirmDialog(null,"Delete Failed!","Delete Failed!",JOptionPane.OK_OPTION);
}
catch(java.lang.ClassNotFoundException ex)
{
System.out.println("class not found!");
}
catch(java.io.IOException exe)
{
System.out.println("IO Exception");
}
finally
{
con=null;
}
jtbEmployee.setModel(new TableModel());
jtbEmployee.updateUI(); }
if(e.getActionCommand().equals("Add"))
{
URL url=new URL("http://localhost:8083/WebApp/server");
con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
fillEmployee();
employee.setAction("Add");
ObjectOutputStream oos=new ObjectOutputStream(con.getOutputStream());
oos.writeObject(employee);
oos.flush();
oos.close();
ObjectInputStream ois=new ObjectInputStream(con.getInputStream());
try
{
employee=(EmployeeData)ois.readObject();
ois.close();
if(employee.getRowChanged())
JOptionPane.showConfirmDialog(null,"Add Successfully!","Add Successfully",JOptionPane.OK_OPTION);
JOptionPane.showConfirmDialog(null,"Add Failed!","Add Failed!",JOptionPane.OK_OPTION);
}
catch(java.lang.ClassNotFoundException ex)
{
System.out.println("class not found!");
}
catch(java.io.IOException exe)
{
System.out.println("IO Exception");
}
finally
{
con=null;
}
jtbEmployee.setModel(new TableModel());
jtbEmployee.updateUI();
}
}
catch(MalformedURLException exe)
{
exe.printStackTrace();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
update,delete,add.ActionEvent事件出错,出错信息如下:
java.net.ConnectException: Connection refused: connect
-----------------我是传说中的分割线----------------
我也是刚刚起步,不知道说的是否正确