开发者社区> 问答> 正文

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

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” 没有走写的代码程序?怎么回事呢?


展开
收起
爱吃鱼的程序员 2020-06-22 17:25:45 653 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    怪异的用法,你的 <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">ServletToBeanProxy是怎么样的。<divclass="ref">

    引用来自“huan”的答案

    <divclass=ref_body>怪异的用法,你的 <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">ServletToBeanProxy是怎么样的。

    去掉这两个注解:

    <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14.399999618530273px;line-height:22px;background-color:#FFFFFF;">@Controller 
    <spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14.399999618530273px;line-height:22px;background-color:#FFFFFF;">@Scope("prototype") 

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

    2020-06-22 17:26:02
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载