servlet程序不报错,但是不出运行结果(java :servlet+Spring)

kuixue 发布于 2013/08/20 16:26
阅读 1K+
收藏 0

servlet+spring  myEclipse

servlet类中:


@Controller
@Scope("prototype")
public class RegistAction extends HttpServlet {
private static final long serialVersionUID = 1L;

//注入Service
@Resource(name="memberService")
private MemberService memberService ;

public void setMemberService(MemberService memberService) {
this.memberService = memberService;
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();

String username = request.getParameter("username").toString();

int flag = memberService.regist(username);
String result = "";
if(flag == 1){
result="注册成功";
}else if(flag==2){
result="该用户已注册";
}else{
result="系统异常";
}

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();

}

service中:

/**
* 注册
*@param LoginPass
*@return 1:注册成功   2.已注册    0.系统异常
*/
public int regist(String username){
int flag=0;
try {
Connection conn = ConnectionUtil.getConn();
String sql = "select username from users where username = '"+ username +"'";
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery(sql);
String tempName="";
while(rs.next()){
tempName=rs.getString("username");
}
//如果没有值,保存用户,并提示注册成功

if(tempName.isEmpty()){
saveEntity(user);
flag=1;
}else{
flag=2;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return flag;
}


private void saveEntity(User user) {
try {
Connection conn = ConnectionUtil.getConn();

String sql = "insert into users (username,password,comfirmpwd,phone,email)"
    +"values('"+user.getUserName()+","+user.getPassword()+","
    +user.getConfirmPwd()+","+user.getPhone()+","+user.getEmail()+"')";

PreparedStatement psmt = conn.prepareStatement(sql);
int count = psmt.executeUpdate(sql);
System.out.println("保存了"+count+"条用户信息");
} catch (SQLException e) {
e.printStackTrace();
}

web.xml中:

<servlet>
    <servlet-name>RegistAction</servlet-name>
    <servlet-class>com.xue.manager.ServletToBeanProxy</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>RegistAction</servlet-name>
    <url-pattern>/RegistAction</url-pattern>
  </servlet-mapping>


beans.xml中:

<bean id="RegistAction" class="com.xue.action.RegistAction"/>

配置了数据源,但是运行不报错,点击页面注册功能,只出现打印信息“This is class com.xue.action.RegistAction, using the POST method” 没有走写的代码程序?怎么回事呢?


加载中
0
huan
huan
怪异的用法,你的  ServletToBeanProxy 是怎么样的。
0
kuixue
kuixue

引用来自“huan”的答案

怪异的用法,你的  ServletToBeanProxy 是怎么样的。

 ServletToBeanProxy 是这样的 :

package com.xue.manager;


import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * 自己实现一个代理类,用来将 Servlet 转换成 由Spring 管理的 Servlet Bean
 */
@SuppressWarnings("serial")
public class ServletToBeanProxy extends GenericServlet {


private String targetBean;   // 当前客户端请求的 servlet名字
private Servlet proxy;       // 代理servlet

@Override
public void init() throws ServletException{
super.init();
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");//初始化spring容器
this.targetBean = getServletName();
this.proxy = (Servlet)ac.getBean(targetBean); // 调用servletBean
proxy.init(getServletConfig());//调用初始化方法,将servletConfig传给Bean

}


@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {

//在service方法中调用bean的service方法 , servlet会根据客户端请求调用相应的请求方法(get/post)
proxy.service(request, response);
}


}

0
huan
huan

去掉这两个注解:

@Controller 
@Scope("prototype") 

kuixue
kuixue
试了一下,去掉还是这样结果,是不是Regi实体Action类哪里写错了?
0
kuixue
kuixue
自己解决了。
返回顶部
顶部