JavaWeb测试环境搭建之javaServlet实现登陆

- 使用Eclipse创建Dynamic web project
输入工程名,然后next 记得选上创建web.xml 也可以后面再创建。
- 编写java Servlet代码
代码如下

//引入所需要的包
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class LoginServlet extends HttpServlet {
    //重写doGet方法
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
     throws ServletException,
            IOException {
        String username = request.getParameter("username");   
        String password = request.getParameter("password");      

        //服务器端打印信息
        //System.out.println("username=" + username);
        //System.out.println("password=" + password);
        //设置编码格式
        response.setContentType("text/html;charset=GB18030");

        //返回html页面
        response.getWriter().println("<html>");
        response.getWriter().println("<head>"); 
        response.getWriter().println("<title>登录信息</title>");    
        response.getWriter().println("</head>");    
        response.getWriter().println("<body>"); 
        response.getWriter().println("欢迎【" + username + "】用户登录成功!!!");  
        response.getWriter().println("</body>");    
        response.getWriter().println("</html>");
        }                   
    //重写doPost方法
    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException,
            IOException {
        doGet(request, response);                   
    }     
}
  • 配置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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
</web-app>
  • 在webcontent目录下创建index.html,内容如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
   <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
       <title>登录</title>  
    </head>  
    <body>  

       <form action="http://localhost:8080/LoginServlet/LoginServlet" method="post">  
          用户:<input type="text" name="username" /><br/>  
           密码:<input type="password" name="password" /><br/>  
          <input type="submit" value="登录" />  

      </form>  
  </body>  
</html> 

这样一个登陆功能就配置好了,右键run as –>run on server 前提是配置好了tomcat服务器。我的已经配置好了,不会就进行百度
浏览器进入:http://localhost:8080/LoginServlet/
这里写图片描述
然后输入用户名和密码登陆
这里写图片描述
这样一个简单的javaWeb应用就开发好了,后面我们就可以对这个应用进行模拟压力测试了

中途可能出现的问题:
1 我拷贝过来的代码index.html前面这个LogingServlet是小写,前面这个是工程名,和工程名一样。后面是javaServlet的名称
http://localhost:8080/LoginServlet/LoginServlet
2 配置好后,点击登陆。出现下面错误
massage: Error instantiating servlet
找不到servlet
首先考虑web.xml有没有配置错误。没有错误就是下面这种原因,java编译生产的class没有在webcontent/class下面。需要进行如下设置
project(选中项目)->properties->java build path->source->src,将Default output folder设置为[项目名]/WebRoot/WEB-INF/classes,点击OK。
没有classes目录创建一个。我遇到的就是这种情况

文章大部分是借鉴别人的,转载地址:
http://blog.csdn.net/jiuqiyuliang/article/details/36424981/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值