Servlets

Hi,

I am trying to create a servlet that will take an input from the user in the form of a button press.

I want an already created html page to be displayed to the user when the servlet is created and capture the button press in order to run some code.

What I need to know is how do I do this, do I specify the location and name of the html page in the doGet method? I also wanted to update the html page with the code that was being run but I am not sure if I can do this. Would I need to create another html page in the doPost method to display this code? Would this be the easiest option?

Sorry I am new to servlets and only have a limited time to create this.

Thank you.

[712 byte] By [bitchinsahsaa] at [2007-11-27 2:44:47]
# 1

[nobr]I didn't understand what do you want exactly.

Here a simple example to start with :

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>

<servlet-name>register</servlet-name>

<servlet-class>web.RegisterServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>register</servlet-name>

<url-pattern>/register</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>register.jsp</welcome-file>

</welcome-file-list>

</web-app>

registerServlet.java

package web;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class RegisterServlet extends HttpServlet {

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

String name = request.getParameter("name");

String email = request.getParameter("email");

System.out.println("name="+name);

System.out.println("email="+email);

//do a business task here : data processing, db access...

request.getRequestDispatcher("/registered.jsp").forward(request, response);

//response.sendRedirect("/page.jsp");

}

}

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form action="/register">

Name: <input name="name" value=""></input><br:>

Email: <input name="email" value=""></input>

</form>

</body>

</html>

registered.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

Name = <%= request.getParameter("name")%><br/>

Email = <%= request.getParameter("email")%>

</body>

</html>

compile the servlet and create/deploy the application war in a app server (tomcat for example).

Use http://127.0.0.1:8080/YOUR_WAR_NAME

hope that helps[/nobr]

java_2006a at 2007-7-12 3:11:44 > top of Java-index,Java Essentials,New To Java...
# 2

Hi,

Sorry if I was not clear!

I need to create a Servlet to run on an embedded webserver and I am not sure how I can do it. I have noticed there are examples with JSP however I have never used this before. Is this the only way to use a html page that has not been created by the servlet or should I just create the html page when the servlet runs?

Also I will have four buttons that will trigger an event. Can I have this? Will I be able to write it into the code if a certain button is triggered then carry out some task or will I need to have a seperate servlet for each of these tasks?

Basically I want to press a button and the servlet send a signal down a serial port to control a robotic arm. I have an initialise sequence and three movement sequences which should correspond to a button being pressed. Once this has been done I want to display the code that moved the arm in order for the user to learn how to move the arm. Can I return code to a html page or do I need to create another one and display it seperately?

Thanks for your help.

bitchinsahsaa at 2007-7-12 3:11:44 > top of Java-index,Java Essentials,New To Java...