您的位置:首页 > 编程语言 > Java开发

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

2016-04-02 21:42 721 查看
- 使用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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet java 测试 web